Combine text files in Linux

How to Combine Text Files on Linux

Are you looking for a way to combine text files on Linux the easy way? Don’t fret, for this quick guide has you covered. 

In Linux, you often want to join the contents of two or more files. And FYI, It is actually possible for files to be deliberately split earlier. For example, you want to transmit to other computers and then merge their contents. This post will be discussing how to combine two or more files in Linux

How to Combine Text Files on Linux

You can combine, append, and add supplemental contents to the file by using the cat and sed commands. We will be elaborating on those two functions in the examples below.

First Approach. Linux cat command

The Linux cat command is generally used to display the contents of a text file. In Linux and the other Unix-like operating systems, this command is already built-in and can be used immediately. To view the contents of a file without actually opening it, you can use this command:

$cat file1.txt

Replace ‘file1’ with the name of the file whose contents you want to see. And then, use the pipe operator ‘|‘ followed by less command if the file’s contents are too large and are difficult to view in one go. This command will display as much content as possible on the screen, and then you can also press ”Enter” to see more.

Input:

$cat file1.txt|less

Combining contents of files

The same cat command can be used to combine two or more text files. You can use the following command to combine the contents of two files and display them on a standard console:

$cat file1.txt file2.txt

Replace ‘file1’ and ‘file2’ with the names of the files, you want to combine. And to merge the three files together, you have to add the name of the third file in the command as follows. The same procedure can be used for more than three files.

Input:

$cat file1.txt file2.txt file3.txt

Redirecting the output of the combined file to another file

Once the files have been joined together, you can redirect the output to a file instead of the standard output, as follows:

$cat file1.txt file2.txt>file.txt

Replace the ‘file.txt’ with the name of the destination file. If the file.txt doesn’t exist, a new file is created. And if the file already exists, it will be overwritten. Now, to view the content of the file.txt, and ensure that it contains the combined contents of ‘file1.txt’ and ‘file2.txt’, use the following command:

$cat file.txt

Appending to an existing file

In order to append to an existing file instead of creating a new file, you can use the ‘>>’ operator as follows:

$cat file1.txt file2.txt>>existing_file.txt

Sort the files before combining

Suppose you want to merge the contents of some files, but you want to sort them before you combine text files on Linux. You can use the sort command with the pipe operator. Then use the redirection operator to save them to a file.

Input:

$cat file1.txt file2.txt file3.txt|sort>file.txt

Saving Adding additional contents to file

Suppose, after merging a few files, you want to add some additional content to the final file. The same cat command can also be used to achieve this objective. Type the cat command, then the redirection operator ‘>>‘ and the name of the file like this:

$cat >>file.txt

You will see that a cursor will appear on the console prompting the user to enter the text to be appended. Type the text you want to append, and then press the ”Enter” key followed by “Ctrl + D”. This will append the content to the file and exit the cat command.

Concatenating a large number of files

Sometimes, the number of files is too large, and it will be hard to specify every one of them by name. For instance, you want to concatenate all the text files in your current directory. To do this, you can use the wild card operator to specify all of them as follows:

$cat *.txt>output.txt

Remember: 

  • If the output file already exists in the current directory, it will be included in the input as well, because of the wild card character, and you will get an error.
  • The order of the files to be concatenated is unpredictable. Therefore, the files will be merged in any order.

Second Approach. Using the sed command

Another approach to merging the contents of two files is to use the sed operator. You can do this by passing the r flag to the command. Doing this will read the files provided as an argument, and then outputs the combined content to standard output. The following command shows how you can use the sed command to combine two or more files:

$sed r file1.txt file2.txt

To redirect the output to a file, you can use the following command:

$sed r file1.txt file2.txt > file.txt

And that’s about it for this article, we discussed how you could combine the output of two or more files in Linux in more than just one way. Primarily, we discussed the cat command and sed command. 

We also talked about the various options of the commands, like how to redirect the command’s output to the file. Additionally, we even tackled how we can append it to a file, and how it can be displayed gradually instead of in one go. If you want more details about the aforementioned commands, feel free to access them on the manual page.

If this guide helped you, please share it.

Leave a Reply
Related Posts