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.
Table of Contents
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
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
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.
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 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
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
That’s all for now! If you are having any issues while following the guide, then do let me know in the comment section.
A man with a tech effusive who has explored some of the amazing technology stuff and is exploring more. While moving towards, I had a chance to work on Android development, Linux, AWS, and DevOps with several open-source tools.