In the world of Linux, ‘cat’ is a versatile and widely used command-line utility that allows you to perform various operations on text files. Short for ‘concatenate,’ the cat command is primarily used for displaying file content, creating new files, and concatenating files.
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 part of GNU utils, which makes it available in all Linux distributions.
Syntax of Cat Command
The syntax of the cat command in Linux is straightforward and easy to understand. The basic structure of the command is as follows:
cat [option] [file]...
- cat: The command itself, which stands for ‘concatenate.’
- [options]: Optional flags that modify the behavior of the command. You can use one or more options in combination, as needed.
- [file(s)]: The file(s) you want to work with. You can specify one or multiple files, depending on the operation you want to perform.
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.
Cat Command Examples
Here are some common examples of using the cat command in Linux:
1. 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.
2. 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.
3. 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.
4. 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.
5. 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.
6. 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.
7. 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.