Combine text files in Debian with the cat command (with examples)

The CAT command on Linux is useful not only for creating text files and displaying their contents, but also for combining text from two or more text files. Then the combined text can be saved in another text file. In this guide, you will learn how to use the CAT command to combine text from two or more files into one. This will help you get advanced Debian user status from the average beginner. We ran the commands mentioned in this guide on a 10-member Debian system.

In this article, we will give you some examples to help you understand the proper use of the CAT command in the following four scenarios:

  • Merge text from multiple text files into one text file.
  • Combine text from multiple files and save the output in another file in alphabetical order.
  • Add text from one text file to another.
  • Adding text from a Debian terminal directly to a text file.

Note. It is recommended that you back up important files before modifying their contents.

Example 1: combining text from three files into another text file

We created three sample text files in our system with the names textfile1.txt, textfile2.txt and textfile3.txt. All of these files contain a string of text. The next use of the CAT command will display text from all of these files in a single output.

Open the Debian terminal through a search in the Application Launcher. You can access the Application Launcher by pressing the Super / Windows key. Then enter the following command syntax to print the contents of three text files:

cat [file1.txt] [file2.txt] [file3.txt]

In the following figure, you can see how the output from my three text files is printed as one combined output:

Linux allows you to print the output of a command to a file using the following syntax:

$ [command] > [filename]

Let’s use this command and the cat command described above to save 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 save the combined text from my three files to a new textfile4.txt file; Then I print the contents of the new file on the screen to view:

Merging Text Files

Remember that if the destination text file already exists on your system, its contents will be overwritten.

Example 2. Merging text from three files and saving the output in another file in alphabetical order.

Suppose you have three text files; each of which contains some text. You want to combine text from all three and save the output to a fourth file, but in alphabetical order. Here’s how you 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 into a new textfile4.txt file, the output will be as follows:

Combine and sort text from files

However, I want the output sorted alphabetically to be output to my text file, so I will use the following command syntax:

$ cat textfile1.txt textfile2.txt textfile3.txt | sort > textfile5.txt

Sort and combine text

You can see how my newly created textfile5.txt contains merged and sorted text from my three source files.

Example 3: adding text from one text file to another

The cat command can also be used to add text from the source file to the destination file without being confused with the contents of a later version.

Here is an example destination file:

sample destination file

Here is an example of the source file:

source file example

The syntax for adding text is:

cat [sourcefile.txt] >> [destinationfile.txt]

Here’s what my final file looks like after adding text from my source file to it:

Add text using cat command

Example 4: adding text from a terminal directly to a file

If you want to add some text from the command line to the end of an existing text file, you can use the following syntax:

$ cat >> [textfile.txt]

After entering this command, a cursor will appear to enter the text that you want to add to the specified file. Enter the text and press Ctrl + D. Your entered text will be added to the end of the file without breaking its existing contents.

Add text from terminal

You can see this text added to the file in the following image:

Resulting text file

We hope that the detailed examples described in this article, as well as the syntax of the cat command in each case, help to merge the contents of several files into one. Moreover, you can outperform sorting and adding text not only from one file to another, but also directly from the Debian terminal.

Combine text files in Debian with the cat command (with examples)

Sidebar