Introduction
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.. In this article, 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]
Example: cat filename.txt
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] ...
For example: cat file1.txt file2.txt
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]
For example: cat -n file1.txt
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.
For example: cat > file4.txt
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]
For example: cat file1.txt > file2.txt
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]
For example: cat -E file1.txt
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]
For example: cat -s file1.txt
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]
For example: cat -T file3.txt
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]
For example: tac file4txt
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]
For example: cat -A file4.txt
Conclusion
We've covered most of the cat commands here, so practicing them should give you a pretty good idea about this command. You can also check our basic commands from the below links.