March 18, 2019
March 18, 2019
The cat command in Linux is one of the most useful commands you can learn. It derives its name from the word concatenate and let you create, merge or print files in the standard output screen or to another file and much more.
It does not require you to install anything since it comes pre-installed with the coreutils package in any Debian or Red Hat based system.
In this tutorial, we will cover the common usages of the Linux cat command explaining its features.
Before we start exploring the article’s subject, we should log into the VPS using SSH, and quickly check the basic syntax. The command takes a filename as an argument along with options to specify particular operations.
cat [OPTION] [FILE]
To find all the available options, just type cat –help from the terminal.
Using the cat command you can quickly create a file and put text into it. To do that, use the > redirect operator to redirect the text in the file.
cat > filename.txt
The file is created, and you can begin populating it with text. To add multiple lines of text just press Enter at the end of each line. Once you’re done, hit CTRL+D to exit the file.
To verify that the file is indeed created by the command used above, just use the following ls command in the terminal:
This is one of the most basic usages of the cat command. Without any options, the command will read the contents of a file and display them in the console.
To prevent scrolling large files, you might want to add the option | more to output through the less or more display:
cat filename.txt | more
You can also display the content of more than one file. For example, to display content of all text files, use the following command in the terminal:
Rather than displaying the contents of a file in the console you can redirect the output to another file using the option >. The command line would look like this:
cat source.txt > destination.txt
If the destination file does not exist then the command will create it, or overwrite an existing one by the same name.
To append the contents of the destination file, use the >> option along with the cat command:
cat source.txt >> destination.txt
This command also lets you concatenate multiple files into a single one. Basically it functions exactly like the redirection feature above, but with multiple source files.
cat source1.txt source2.txt > destination.txt
Like earlier, the above command will create the destination file if it does not exist, or overwrite an existing one with the same name.
The cat command can also mark line ends by displaying the $ character at the end of each line. To use this feature, use the -E option along with cat command:
cat -E filename.txt
With the cat command you can also display the contents of a file along with line numbers at the beginning of each one. To use this feature, use the -n option with cat command:
cat -n filename.txt
To display all non-printable characters use the -v option along with cat command like in the following example:
cat -v filename.txt
To display tab characters only, use -T:
cat -T filename.txt
The tab characters will be shown as ^I
To suppress repeated empty lines, and safe space on your display you can use the -s option. Keep in mind that this option will keep one blank line by removing the repeated empty lines only. The command would look like this:
cat -s filename.txt
To display non-empty lines with line numbers printed before them use the -b option. Remember the -b option will override the -n option:
cat -b filename.txt
To view the contents of a file in reverse order, starting with the last line and ending with the first, just use the tac command, which is just cat in reverse:
That’s it. You now know all the basic features and functions of the cat command. You will now have the basic understanding to put it to good use. For more information on the cat command, you can always invoke manual page of cat with the command man cat !.
We hope this article helped you better your Linux Terminal skills. See you in the next one!