Let’s look at the different ways in which you can merge multiple files in Linux. We’ll majorly use the cat command for this purpose. So let us begin!
For the rest of this tutorial we will consider three files. Let’s create these files:
We’ll use the cat command to create these files, but you can also use the touch/nano command to create and edit the files.
$ cat > [filename]
Displaying the content together
Since cat command is short for Concatenate, it is the first go to for concatenating the content together.
$ cat file1.txt file2.txt file3.txt
Note that the order in which the content appears is the order in which the files appear in the command. We can change the order in the command and verify.
Merge multiple files in Linux and store them in another file
To store the content that was displayed on the screen in the previous example, use the redirection operator. (>)
$ cat file1.txt file2.txt file3.txt > merge.txt
The output has been stored in a file. An important thing to note here is that cat command would create the file first if it doesn’t exist. The single redirection operator will overwrite the file rather than appending at the end. To append the content in the end consider the next example.
Appending content to an existing file
To append content after you merge multiple files in Linux to another file, use double redirection operator. (>>) along with cat command.
$ cat file1.txt file2.txt file3.txt >> merge.txt
Rather than overwriting the contents of the file, this command appends the content at the end of the file. Ignoring such fine detail could lead to an unwanted blunder.
Using sed command to merge multiple files in Linux
Sed command, primarily used for performing text transformations and manipulation can also be used to merge files.
$ sed h file1.txt file2.txt file3.txt > merged.txt
The content from the files is stored in the hold buffer temporarily, usually used to store a pattern. It is then written to the specified file.
Automating the process using For loop
For loop can save the effort of explicitly mentioning the file names. This will only work if the filenames follow a pattern. Like in our case the file names follow the pattern: file{1,2,3}.txt. This can be used to take advantage of for loop.
$ for i in {1..3}; do cat "file$i.txt" >> out.txt; done
The code simply exploits the fact that the files are named in a similar pattern. This should motivate you to think about how you want to name your files going forward.
Conclusion
In this tutorial, we covered some of the ways to merge multiple files in Linux. The process of merging is not exclusive to text files. Other files such as logs, system reports can also be merged. Using For loop to merge files saves a lot of effort if the number of files to be merged is too large.