Basic Cat Commands in Linux with Examples
Description
The cat (short for “concatenate“) commands is one of the most frequently used command in Linux/Unix like operating systems.
cat command allows us to create single or multiple files, view contain of file.
We will discuss about cat commands in detail.
1. How to view contents of a file using cat
The cat commands allow you to view content of a file on the standard output (stdout).
Check this with the below command.
cat [filename]
[root@vps~]# cat file1.txt Testing file 1 [root@vps~]#
2. How to display multiple files using cat
With cat commands we can also check contents of multiple files in one go.
Check this with the below command.
cat [filename] [filename] ...
[root@vps~]# cat file1.txt file2.txt Testing file 1 Testing file 2 [root@vps~]#
3. How to display contents of file with line numbers
Also you can display contents of a file preceding with line numbers.
This can be done by using the tool’s -n command line option.
cat -n [filename]
[root@vps~]# cat -n file1.txt 1 Testing file 1 2 [root@vps~]#
4. How to create file using cat command
If you want, you can also create a new file, and fill it with information using a single cat command.
Check the below command to know how to execute this.
cat > [name-of-new-file]
When run, the command requires you to enter the information on the terminal.
Once you’re done, just press CTRL+d.
cat > file3.txt
[root@vps~]# cat > file3.txt Hi Testing file 3 [root@vps~]#
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]
cat file1.txt > file2.txt
[root@vps~]# cat file1.txt > file2.txt [root@vps~]# cat file2.txt Testing file 1 [root@vps~]#
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]
cat -E file1.txt
[root@vps~]# cat -E file1.txt Testing file 1 $ [root@vps~]#
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]
cat -s file1.txt
[root@vps~]# cat file1.txt Testing file 1 Testing file 2 Testing file 3 [root@vps~]# cat -s file1.txt Testing file 1 Testing file 2 Testing file 3
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]
cat -T file3.txt
[root@vps~]# cat -T file3.txt Testing ^I ^File ^I^Created [root@vps~]# cat file3.txt Testing File Created [root@vps~]# cat -T file3.txt Testing ^I ^File ^I^Created
9. View the Contents in Reverse Order
If you want, you can also make the cat command display content in reverse order.
This can be done using the tac command line option.
tac [filename]
tac file4.txt
[root@vps~]# cat file4.txt Testing File Created [root@vps~]# tac file4.txt Created File Testing
10. The -A option
In case, you need to use the -E and -T option together, then instead of writing -ET in the command, you can just use the -A command line option.
cat -A [filename]
cat -A file4.txt
[root@vps~]# cat file4.txt Testing File Created [root@vps~]# cat -A file4.txt Testing$ ^File$ Created $
We hope you’ve found this useful!