We continue to move forward with the Linux terminal and its tools to perform operations on files and folders. Well, today it is the turn of another command belonging to GNU utils as it is the Linux cat command.
In this post, we will tell you the generalities of the command, as well as its syntax and options. Besides, you will learn how to use it thanks to several examples.
The Linux Cat command
The Linux cat command is a Unix family command for working with text files. With the cat command we can do three things:
- Display the contents of a file or several
- Combine the contents of several files
- Create a new file.
However, even though the cat command provides us with these possibilities, it is mainly used to display the contents of a file.
The cat command is also part of GNU utils, which makes it available in all Linux distributions. Therefore to use it you only need to open a terminal.
Using the Linux cat command
Like most commands in the Unix family, the command has a basic syntax to follow. In the case of the cat command, the syntax is as follows:
cat [option] [file]...
Also, the options modify the behavior of the command. It is in the options where most of the command’s possibilities lie.
The options of the cat command are presented in the following table:
|Option||Description according to GNU utils manual|
|-A or –show-all||Shows all the file information|
|-b or –number-nonblank||Number all nonempty output lines, starting with 1.|
|-E or –show-ends||Display a ‘$’ after the end of each line.|
|-n or –number||Number all output lines, starting with 1. Note: this option is ignored if -b is in effect.|
|-s or –squeeze-blank||Suppress repeated adjacent blank lines|
|-T or –show-tabs||Display TAB characters as ‘^I’|
|-v or –show-nonprinting||Display control characters except for LFD and TAB using ‘^’ notation|
However, with some examples, we will be able to show the usefulness of the command. So let’s go for it.
Viewing the contents of a file with the cat command
To display the contents of a text file, use the following syntax.
:~$ cat [file]
If the file is not at the same location as the prompt you have to specify the entire path.
:~$ cat /file/path
In this case, I have created a text file for the test.
:~$ cat file1.txt
As you can see in the picture, the whole file has been shown.
On the other hand, it is possible that the content of a file is very large and to avoid scrolling around the terminal it is possible to use more and less.
:~$ cat file1.txt | more
:~$ cat file1.txt | less
This will make it easier for you to navigate and read.
It is also possible to display the contents of two or more files with the cat command, just specify them separated by a blank space.
:~$ cat file1.txt file2.txt
And so you can view several text files at once.
Display the number of lines in the file
When the file is large enough and you want to know the number of lines it has, the cat command makes it possible by adding the -n option.
Only, it is necessary to add it in the following way:
:~$ cat -n [file]
:~$ cat -n longfile.txt
As you can see, this file has eleven lines.
Displays the number of non-blank lines with the command cat
As you may have noticed, the previous file has 11 lines but the empty lines are included. To show only the lines that have text, use the -b option.
This way the command looks like this:
~$ cat -b longfile.txt
Remember that option -b overrides option -n.
Mark the end of the line with a $
Sometimes it is useful to know when a line ends, this can be done with the command cat.
To do this, add the -e option to the command and it will look like this:
:~$ cat -e longfile.txt
As you can see in the image, a $ is shown at the end of each line even if it is blank.
The cat command also displays the non-printable characters and the tabs
It not only shows the content of a text file but also those things in the file that we do not see like non-printable characters and tabs.
For the non-printable characters, you can use the -v option. If there are any, it will indicate them with ^.
:~$ cat -v longfile.txt
And for the tabulations, the option is -T. So in capital letters.
:~$ cat -T longfile.txt
In this case, it indicates them with ^I.
Concatenating several files into one
Concatenating several files into a new one is quite simple with the cat command. This utility is another of the command’s great features.
It uses the following syntax:
:~$ cat [file1] [file2] > [newfile]
In this case, the command will take the files before the > sign and merge them into a file with the name and location you want.
Let’s look at the contents of the files:
:~$ cat file1.txt file2.txt
Now run the following command:
:~$ cat file1.txt file2.txt > file3.txt
Then, display the contents of file3.txt
:~$ cat file3.txt
As you can see, the two files have been merged into one.
Creating a new file with the cat command
The other utility of the cat command is to create files. To do this, simply use the > operator and give the new file a name.
Note that the file must not exist because otherwise, the cat command will overwrite the entire contents.
To create a file with its extension, use the following command
:~$ cat > [file]
:~$ cat > test.txt
And just start writing. When you are done, exit the editor by pressing the CTRL + D keys.
Then, display the contents of the file to check.
:~$ cat test.txt
- Creating a new file with the cat command
So you can create files with the cat command.
If you want to modify this file with the command cat, you have to use the >> operator because if you repeat the command with > it will delete the content.
:~$ cat >> test.txt
Add the changes and exit again with CTRL + D. And display the file contents again.
:~$ cat test.txt
That’s how easy it is to create text files with the command cat.
In this post, we have covered quite explicitly the use of the cat command. The utility of this command lies mainly in displaying the contents of a text file. Thanks to the options, we can have several behaviors that improve the experience with the command.
Finally, we invite you to read our post about the cp command. Another easy to use file manipulation command.