How to Combine Two Text Files in Linux

In the course of work, I have received several redirected command output files from my colleague, and he asked me to combine all the files into one file and send it back. And I’m not in the mood to open all the files one by one to read the output and combine them into one file.

So I have to find a way through which I can combine multiple text files into one, and then I can comfortably drink a cup of coffee.

I have come up with the idea of merging all the text with the redirection operator, or else I can use the sed command to pass the content as a readline argument, which will combine the multiple texts into one.

So let me start with the basic approach to adding two texts or many files into one text using the cat command, and later we will jump to the head, more, and sed commands.

Combine Two or more Text Files using cat command in Linux

When you just want to read two or more text files combined as one, then you can use the cat command, which is used to read file content on a terminal.

Usually, we provide only one file to read content with the cat command. Instead of that, add as many files as you want to read file content on your terminal.

Let’s say you have a two-sample file named “sample_1” and “sample_2” and want to read it after combining both contents on a terminal, then you need to pass the command like the below snippet:

$ cat sample_1 sample_2
Read multiple combine fine on terminal
Combine result

And when you are confirmed with the combined output, you can redirect the output to the new file and access it whenever you need to use it.

$ cat sample_1 sample_2 > combine_sample 

If you want to append the combined output to an already existing file without having it overwritten, use the >> redirection operator:

$ cat sample_1 sample_2 >> combine_sample 
Save multiple combine text to single file
Save multiple combined texts to a single file

With the above command, the output will get merged beneath the prior content, but what do If I want to add content before the previous stored file, for this we have dedicated our please read that.

I’m fed up with you humans. You always overuse me.

Combine Two or more Text Files using the head command in Linux

With the above method, there is a problem in that you cannot identify which file output has been extracted to the terminal. I think it would be better if we could add a header on top of every file section.

To add a header, you can use the head command, which will add the file name on top of every file.

From the above example, you do have two sample files, “sample_1” and “sample_2”, and you want to combine them along with the filename, then you need to invoke the below code.

$ head sample_1 sample_2
Combine Two or more Text Files using the head command in Linux
Combine Two or more Text Files using the head command in Linux

Combine Two or more Text Files using more commands in Linux

When you combine multiple files with a cat command, you may find it difficult to scroll content forth and back. To fix this, you can use more commands which will add the scrolling capability.

For example, if you have more than two text files and you want to combine them with a scrolling feature, then use the below syntax to achieve your desired result.

$ more sample_1 sample_2

You can also redirect output to a new file using redirection operators > or >> according to your usage.

$ more sample_1 sample_2 > combine_sample

And to read the output, you can use:

$ more combine_sample

Combine Two or more Text Files using the sed command in Linux

You can also use the sed command to merge two or more text files into one, which is as simple as the above command.

For example, I have two sample files from the above example, which I want to merge into one. To do this, I need to run the following command:

$ sed R/r sample_1 sample_2
Combine Two or more Text Files using sed command in Linux
Combine Two or more Text Files using the sed command in Linux

And once we are satisfied with the output, let me create a new file where I can save the new changes.

$ sed R/r sample_1 sample_2 >> combine_sample
or
$ sed R/r sample_1 sample_2 > combine_sample

To view content, you can use the cat command. If the content is big enough to fit on the screen, then use the pager command less or more to add scrolling capability.

$ cat combine_sample
$ more combine_sample
$ less comine_sample
Finish_gif

That’s all for now! If you are having any issues while following the guide, then do let me know in the comment section.

This Post Has 2 Comments

  1. LinuxUser

    “head” by default only writes the first 10 lines of each file. When used with several files you will get header lines, but not the complete files (if they have more than 10 lines).

    What is actually the meaning of “R/r” for sed? The following (even simpler example) would also work (giving sed a quoted nothing):

    $ sed '' sample_1 sample_2

    1. Gagan Mishra

      Thanks for pointing about the head limitation.
      R/r basically used to provide files as an argument

      r filename
      Reads file filename.

      R filename
      Queue a line of filename to be read and inserted into the output stream at the end of the current cycle, or when the next input line is read.

      Anyway, thanks for the command.

Leave a Reply