The ‘cat’ [short for “concatenate“] command is one of the most frequently used commands in Linux and other operating systems. The cat command allows us to create single or multiple files, view contain of file, concatenate files and redirect output to the terminal or to files.
The command is available in Linux, Windows Power Shell, and MAC-OS.
Here is the Windows usage screen for cat:
Here is a Linux cat help screen:
We understand there are lots of options, now let’s look at some usage examples.
1. The most basic and repetitive use of cat is to display the contents of a file. If you are using Linux, the following example will show contents of /etc/passwd file:
# cat /etc/passwd
2. You can use cat to create a simple file. Let’s create a file called ‘example1’:
# cat >example1
Now simply enter the information you want in the file followed by the CTRL+D (hold down Ctrl Key and type ‘d‘) to exit. The information you typed will be written into the example1 file.
You can verify this using the following command:
# cat example1
3. We can take that a step further and view content of multiple files where ‘example1’ and ‘example2’ are files:
# cat example1 example2
4. Another way to display multiple files is to use the ‘;’ between cat commands:
# cat example1; cat example2; cat example3
5. Sometimes the output of displaying a file runs off the screen. Managing how much information the cat command displays is simple. The cat command comes with the ability to control the screen output with ‘more’ or ‘less’:
# cat bigfile.txt | more
One screen worth of the file will be shown at a time with the “More” word at the bottom. Just hit the space bar to display the next screen.
# cat bigfile.txt | less
This time the “:” will be displayed. Just hit ‘Q’ to quit.
6. In the old days, line numbering was important in programming. Today, sometimes when helping someone find a particular item, it is helpful to have line numbers in the display. The cat command can use the ‘-n’ option to display line numbers:
# cat -n bigfile.txt
7. You can use cat to redirect output from the terminal window (default output) to say another file. This is called the redirection operator. This is done with the ‘>‘ (greater than) symbol. Be careful here, as you may overwrite an existing file!!
# cat example1 > example3
Essentially we have created a copy of example1 in example3.
8. OK – so what is we had an existing example3 file and we wanted to append example1 contents onto example3? The cat command supports this with the ‘>>‘ (double greater than) symbol.
# cat example1 >> example3
9. Taking that a step further, what is we wanted to send contents of multiple files and create a single file? The cat command does this as follows:
# cat example1 example2 example3 > example4
These files are added in order.
10. Taking that process even further, what if we wanted to do the same thing as #9 but this time sort the contents? The cat command can do this easily with piping to the ‘sort’ process:
# cat example1 example2 example3 | sort > example4
11. OK – what if we wanted to create a file with some set of commands we use regularly and that as input? The cat command does this with the ‘<‘ (less than symbol):
# cat < example5
Great stuff. We hope we have you off and running with the cat command. We certainly did not cover every option, and encourage you to learn more about this powerful little command.
What other uses of the cat command would you add to our list? Add a comment below!
I hope you find this article and its content helpful. Comments are welcomed below. If you would like to see more articles like this, please support us by clicking the patron link where you will receive free bonus access to courses and more, or simply buying us a cup of coffee!, and all comments are welcome!