The CAT command in Linux is not only helpful in creating text files, displaying their contents, but also in merging text from two or more text files. The merged text can then be saved to another text file. In this tutorial, you will learn the use of the CAT command to combine text from two or more files into a single one. This will help you in achieving a power user status on Ubuntu from an average beginner. We have run the commands mentioned in this tutorial on an Ubuntu 18.04 LTS system.
Let us present a few examples in this article which will help you in understanding the proper usage of the CAT command in the following four scenarios:
- Merging text from multiple text files to a single text file.
- Merging text from multiple files, and saving the output to another file in alphabetical order.
- Appending text from one text file to another.
- Appending text from the Ubuntu Terminal directly to a text file.
Note: It is a good practice to backup important files before altering their contents.
Example 1: Merging text from three files to another text file
We have created three sample text files on our system by the name of textfile1.txt, textfile2.txt, and textfile3.txt. All of these files contain a line of text. The following use of the CAT command will display the text from all of these files in a single output.
Open the Ubuntu Terminal by either pressing CTRl+Alt+T or through the Dash, and then enter the following command:
$ cat [file1.txt] [file2.txt] [file3.txt]
In the following image you can see how the output from my three text files is printed as a single merged output:
Linux allows you to print the output of a command to a file by using the following syntax:
$ [command] > [filename]
Let us make use of this command and the cat command to save the text from three different text files to a new text file:
$ cat [file1.txt] [file2.txt] [file3.txt] > [file4.txt]
In the following image, I am saving the merged text from my three files to a new file textfile4.txt; I am then printing the contents of the new file to the screen for you to view:
Please remember that if the destination text file already exists in your system, its contents will be overwritten.
Example 2: Merging text from three files, and saving the output to another file in alphabetical order
Suppose you have three text files; each containing some text. You want to merge the text from all three and save the output to a fourth file, but in alphabetical order. This is how you will do it:
$ cat [file1.txt] [file2.txt] [file3.txt] | sort > [file4.txt]
In the following image, you can view the text from each of my text files. If I simply combine the text to a new file textfile4.txt, the output will be as follows:
However, I want an alphabetically sorted output to be printed to my text file, so I will use the following command:
$ cat textfile1.txt textfile2.txt textfile3.txt | sort > textfile5.txt
You can see how my newly created textfile5.txt contains merged and sorted text from my three source files.
Example 3: Appending text from one text file to another
The cat command can also be used to append text from a source file to a destination file without messing up with the contents of the later.
Here is a sample destination file:
Here is a sample source file:
The syntax for appending text:
$ [sourcefile.txt] >> [destinationfile.txt]
Here is how my destination file looks after I append the text from my source file to it:
Example 4: Appending text from the Terminal directly to a file
If you want to append some text, from the command line, at the end of an already existing text file, you can use the following syntax:
$ cat >> [textfile.txt]
After entering this command, a cursor will appear for you to enter the text you want to add to the specified file. Enter the text and press Ctrl+D. Your entered text will be appended at the end of the file without disturbing its already existing contents.
You can see this text added to the file in the following image:
We hope that the detailed examples described in this article along with the syntax of the cat command in each case will help in merging the contents of multiple files into a single one. Moreover, you can excel sorting and appending of text not only from one file to another but also directly from the Ubuntu Terminal.