One of the most common tasks in the life of a system administrator is viewing text files or configuration files on the command line. There are various ways of achieving this including using the nano or vi/vim text editor. But such command-line editors are typically used when you want to edit the text file. Another alternative is the Linux cat command.
In this tutorial, we will demonstrate how to use the cat command in Linux.
Prerequisites
- Any Linux or UNIX-like operating system.
- Access to the terminal.
- Basic knowledge of using terminal.
What cat command do in Linux
The cat command, short for concatenate, is one of the most popular Linux commands used to display or view the contents of a file. Apart from that, you can use it to create text files, overwrite and even append text to an already existing file.
Cat is normally used to view short files, however for large file viewing recommend using more or less command. For more advanced features vi or nano editors can be used.
A good benefit of cat is that after opening the file you can comfortably use the scroll bar with the mouse if the file size is big.
The cat command takes the following command syntax:
cat options filename(s)
How to use cat command
The very basic command usage is to view a text file. Type cat
followed by the filename, for example:
cat file1.txt
will print the contents of a file, here the filename is file1.txt
.
Most frequently used cat command examples
In addition to just viewing text files, cat command provides a wide selection of options to interact with text files. Here are some of the most frequently used command examples.
1. Create a new file
To create a file, type cat
followed by the greater than sign ( > ) then the text file, and press ENTER.
cat > myfile1.txt
On the new line, type the desired text in the file. Press ENTER and then press Ctrl key + d
to exit the prompt. The cat command reads the input and writes it on the file.
You can also create a file in the command line or a script:
cat > flowers.txt << EOF rose tulip lily EOF
The file content is the lines between the << EOF
and EOF
. The flowers.txt
file will contain the following lines:
apple
orange
lemon
If flowers.txt
did not exist before, it will be created. If it existed before, it will be overwritten.
2. View or display file contents
To view the contents of a text file , for example myfile1.txt
in this case, execute the following cat command.
cat myfile1.txt
If the file has non-ASCII characters you can view with cat -v
command:
cat -v filecode.txt
3. View or display the contents of two or more files
If you wish to view the contents of multiple text files at a go, simply specify the files in one line separated by a space. In this example, we have displayed the contents of two files, one after the other.
cat myfile1.txt myfile2.txt
4. Redirect contents of one file to another file
Using the redirection operator or greater-than sign > you can redirect contents from the standard input file to another text file. In this example, we have redirected or copied the contents of file 1 which is myfile1.txt
to file 2 which is myfile3.txt
.
cat myfile1.txt > myfile3.txt
You can, thereafter, display the contents of the destination file using the following cat command.
cat myfile3.txt
If the destination filename is not present, a new file is created upon running the command. In addition, if the destination file contains some data, it will be overwritten. As such, be careful when using the redirection symbol.
5. Redirect contents of multiple files to a single file
We have seen how to redirect the contents of a single file to another file. In addition, you can redirect or save the contents of one or more files to another single file as follows.
Here, we concatenate the contents of two files: myfile1.txt
and myfile2.txt
to the myfile3.txt
file
cat myfile1.txt myfile2.txt > myfile3.txt
If you preview the file myfile3.txt
, you will notice that it contains the combination of text from the first and the second text file.
cat myfile3.txt
cat
can also be used to concatenate files along with other commands using pipe. For example:
cat file1 file2 | grep foo
Files compressed by gzip can be directly concatenated into larger gzipped files:
cat file1.gz file2.gz file3.gz > combined.gz
6. Append content to a text file
We have seen that the redirection symbol overwrites the destination. Suppose you only want to add or append content to a file without overwriting it. How do you go about it?
To append content to another file, use the double greater-than >> operator.
In the example below, we have appended the contents of myfile2.txt
to myfile4.txt
.
cat myfile2.txt >> myfile4.txt
The second line represents the appended content from the myfile2.txt file.
7. Append / Add text to an existing file
Furthermore, you can append text to an already existing file without redirecting it from another file. To do so, simply type cat
followed by >> and then the file name and press ENTER.
cat >> myfile4.txt
Next type the content you want to add and press Ctrl key + d
to save and append the content to the file.
8. View contents of a file in reverse order
If you are a bit adventurous, you can display the contents of a file in reverse order, i.e. from the last line to the first line of a txt file using the tac command.
tac myfile3.txt
9. Numbering output of a file
Reading long files can be challenging especially if you are searching for a particular line. A better approach would be to number the output lines. The -n option allows you to display line numbers on the standard output in a numeric sequence.
cat -n myfile4.txt
10. Cat command with ‘more’ and ‘less’ options
When viewing a large file, the Linux cat command typically takes you to the end of the file. This makes it difficult to view the file starting from the top. To comfortably scroll down screen by screen, then pipe the output to more as follows.
cat /etc/ssh/ssh_config | more
This allows you comfortably view a large file without a hassle.
The less command is similar to the more command only that it is a bit advanced and lets you scroll page by page with the ability to move back and forth.
11. Display end of a line
To display the end of a file, use the -e
flag using the following command.
cat -e myfile2.txt
The -e
option instructs cat command to suffix the end of the line with a dollar sign ( $ )
12. Display TAB characters in lines
You can easily display tab characters contained in lines using the -t flag. Here, we have created a file called myfile2.txt
with two tab spaces.
The -t
option instructs cat to indicate tab spaces by the ^I
notation.
cat -t myfile5.txt
13. Delete repeated output blank lines in a file
If you have a file with huge gaps between lines, you can get rid of repeated empty output lines using the -t
option.
cat -s myfile5.txt
Useful Cat command options
Here are some of the additional command options:
Options | Description |
---|---|
-A, --show-all | equivalent to -vET |
-b, --number-nonblank | number nonempty output lines, overrides -n |
-e | equivalent to -vE |
-E, --show-ends | display $ at end of each line |
-n, --number | numbers all output lines |
-s, --squeeze-blank | suppress repeated empty output lines |
-t | equivalent to -vT |
-T, --show-tabs | display TAB characters as ^I |
-u | (ignored) |
-v, --show-nonprinting | use ^ and M- notation, except for LFD and TAB |
--help | display other command options |
--version | Prints version information and exit |
Conclusion
In this tutorial, we have shown you how to use the cat command in Linux and listed commonly used command-line options. These will work with Linux or UNIX-like operating systems. The cat command is the ideal tool to go for when you just want to view a file’s contents instead of opting for a text editor such as nano. For more information type cat --help
from the terminal.
Comments