If you are a regular Linux command line user, I am sure you must have used the cat command. The tool is mostly used for displaying the contents of a file, although it provides many other small but useful features. This article will discuss the cat command in detail, explaining some of its key features.
All examples mentioned in this tutorial have been tested on Ubuntu 22.04 LTS.
Linux cat command
The tool's man page describes it as follows:
cat - concatenate files and print on the standard output
Following are 10 points that'll give you a good idea about cat, including how it works and what features it provides:
1. How to view contents of a file using cat
The cat command allows you to view contents of a file on the standard output (stdout). This can be done in the following way:
$ cat [filename]
For example:
$ cat file1.txt
2. How to display multiple files using cat
The tool also allows you to display the contents of multiple files in one go. This can be done in the following way:
$ cat [filename] [filename] ...
For example:
$ cat file1.txt file2.txt
3. How to display contents of file with line numbers
If you want, you can also display the contents of a file with line numbers printed at the beginning of each line. This can be done using the tool’s -n command line option.
$ cat -n [filename]
For example:
$ cat -n file1.txt
4. How to create file using cat command
You can also create a new file and fill it with information using a single cat command. Here's how you do it:
$ cat > [name-of-new-file]
When run, the command requires you to enter the information on the terminal. Once you're done, press CTRL+d.
For example:
$ cat > file4.txt
To cross-verify the existence of the file as well as check what information it contains, I used the following command:
5. How to copy the contents of one file to another file
You can also use cat to copy the contents of one file to another file. This can be done in the following way:
$ cat [filename-whose-contents-is-to-be-copied] > [destination-filename]
For example:
$ cat file1.txt > file3.txt
Please note that the output redirection operator (>) we've used in this case flushes the destination file before the content is moved to it. So, it's worth knowing that another redirection operator (>>) exists that appends the contents to the destination file instead of flushing it first.
6. How to make cat highlight line-ends
If you want, you can also make cat highlight the end of lines. The tool does this by displaying $ at the end of each line. This feature can be accessed using the tool’s -E command line option.
$ cat -E [filename]
For example:
$ cat -E file1.txt
7. How to make cat suppress repeated empty lines
If you want, you can also make the cat command suppress repeated empty lines in output. This can be done by using the -s command line option.
$ cat -s [filename]
For example:
$ cat -s file6.txt
The first command in the above screenshot shows all the empty lines in the file. But due to the -s command line option we used in the second cat command, the repeated empty lines got suppressed.
8. How to make cat display tab characters as ^I
The cat command also allows you to display tab characters as ^I. This can be done by using the tool’s -T command line option.
$ cat -T [filename]
For example:
$ cat -T file7.txt
As you can see in the above screenshot, the first time around TABs were displayed normally, but when the same command is executed with the -T option, TABs got replaced with ^I.
9. How to make cat display non-printing characters
You can also make the cat command display non-printable characters if you want. This can be done using the -v command line option.
$ cat -v [filename]
For example:
$ cat -v file9.txt
As you can see in the above screenshot, the first command normally displayed the contents of the file containing non-printable characters. But when we used the -v option, those characters were displayed in a special notation the option uses.
Note that LFD (line feeds) and TAB characters are exceptions for this command line option.
10. The -A option
If you need to use the -v, -E, and -T options together, then instead of writing -vET in the command, you can use the -A command line option.
$ cat -A [filename]
For example:
$ cat -A file10.txt
As you can see in the screenshot above, the first command shows the file (file10.txt) contains two lines. Those lines contain tabs as well as some non-printable characters. So, when the same command was run with the -A command line option, tabs got replaced by ^I, and non-printing characters were displayed in a special notation. And finally, each line ended with a $. So, effectively, -A did what -vET would have done.
Conclusion
We've covered most cat command options here, so practicing them should give you a pretty good idea about the tool. Do try them out, and once done, head over to the command's man page to learn more about them.