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

s2 0

The CAT command under Linux is useful not only for creating text files and displaying their contents, but also for merging text from two or more text files. The merged text can then be saved in another text file. In this tutorial, you will learn how to use the CAT command to merge text from two or more files into one. This will help you get to power user status on Debian by the average beginner. We ran the commands mentioned in this tutorial on a Debian 10 buster system.

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

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

Note: It is a good idea to back up important files before changing their contents.

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

We have created three sample text files with the names textfile1.txt, textfile2.txt and textfile3.txt on our system. All of these files contain a line of text. The following use of the CAT command displays the text from all of these files in a single output.

Open the Debian Terminal from the Application Launcher search. The Application Launcher can be called up 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]

The following image shows how the output from my three text files will be printed as a single merged 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 the text from three different text files into a new text file:

$ Cat [file1.txt] [file2.txt] [file3.txt] > [file4.txt]

In the following picture, I am saving the merged text from my three files in a new file textfile4.txt; I then print the contents of the new file on the screen for you to view:advertising

Please remember that if the target text file already exists in your system, its content 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 contains a text. You want to merge the text of all three and save the output in a fourth file, but in alphabetical order. Here’s how you will do it:

$ Cat [file1.txt] [file2.txt] [file3.txt] | sort> [file4.txt]

In the image below, you can view the text from any of my text files. If I just combine the text into a new textfile4.txt file, the output looks like this:

Merge and sort text from files

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

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

Sort and merge text

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 target file without confusing the contents of the later file.

Here is an example target file:

Sample target file

Here is a sample source file:

Sample source file

The syntax for appending text:

$ Cat [sourcefile.txt] >> [destinationfile.txt]

This is what my target file looks like after appending the text from my source file:

Append text with cat command

Example 4: Attaching text from the terminal directly to a file

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

$ Cat >> [textfile.txt]

After entering this command, a cursor will appear in which you can enter the text you want to add to the specified file. Enter the text and press Ctrl + D. Your entered text will be appended to the end of the file without disturbing the already existing content.

Append text from the terminal

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

Resulting text file

In any case, we hope that the detailed examples in this article, along with the syntax of the cat command, will help you merge the contents of multiple files into a single one. In addition, you can excel at sorting and appending 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)