The Linux cat command is used to print the contents of a text file. With the Linux cat command, you can print the contents of your c, java source file, Linux configuration files etc.
The cat command is available in every Linux distribution out there by default. So, you don’t have to install it separately.
In this article, I am going to show you how to use the Linux cat command. So, let’s get started.
Basic Usage of Linux Cat Command:
The basic and most common use of the Linux cat command is to use it without any command option.
For example, to view to contents of the /etc/hosts directory, run the cat command as follows:
As you can see, the contents of the /etc/hosts configuration file are printed on the screen.
Printing Line Numbers:
Let’s say, you want to print the contents of a Java source file on the terminal. You can use the cat command of course. But the cat command does not show line numbers by default. For a source file or a program, it is essential. Luckily, the cat command has -n option which you can use to display line numbers.
To display the contents along with the line number of the Java source file Welcome.java, run the Linux cat command as follows:
As you can see, the line numbers are displayed.
Numbering Only Non Blank Lines:
If you want to show line numbers for the lines that are not blank only, you can use the -b option of the Linux cat command.
In the previous Java source file Welcome.java, I’ve added some blank lines just to demonstrate how the -b option works.
As you can see, with the -n option, all the lines (including blank lines) are numbered.
With the -b option, only the lines that are not blank are numbered as you can see in the screenshot below.
Removing Repeating Empty Lines:
A file you’re trying to view may have lots of empty lines one after the other. This will make the output of cat command very long and annoying.
You can use the -s option of the Linux cat command to remove repeated empty lines as follows:
Printing Tab Characters:
In a source code file of a program, you may have used many tab characters. Fortunately, they are invisible by default. But, if you really need to see all the tab characters you have on your file, then you can use the -T option of the Linux cat command.
Where you might need this feature is when you want to replace all the tab characters with white spaces and you want to make sure that there are not any tab characters left.
To display all the tab characters in our Welcome.java source file, the Linux cat command can be used as follows:
As you can see, the tab characters are displayed as ^I.
Printing End of Line Characters:
If you want to print the EOL (End of Line) character which is represented by $, you can use the -E option of the Linux cat command.
For example, to print the EOL characters of Welcome.java, run the Linux cat command as follows:
As you can see, the EOL characters are printed.
Printing Non-Printing, Tabs, and EOL Characters:
Earlier, you had to use the -v option to print the non-printable characters, use the -T option to print the tab characters, and use the -E option to print the EOL characters. What if you need to print all of these? Well, you can combine all of these options together as follows:
But there is a better solution. The Linux cat command has a -A option that does just the same thing with less typing.
As you can see, the outputs are the same.
So, that’s basically how you use Linux cat command to display text files on Linux. Thanks for reading this article.