[How To]: Linux Cat Command – Usage and Examples

Linux Cat Command


Cat or “concatenate files and print on the standard output” is a Linux command to print file content on the standard output like screen. This is one of most popular Linux commands. I think that every Linux administrator has executed the below command at least one time 😉 :

# cat /var/log/messages

So this is actually very useful and popular command.

Usage and Examples


Cat command has few options but has lot of usages.

Show file content: If Cat command run without options, it will shows you the content of the file or files.

Linux Cat Command – Show Single File Content

Show multiple files content: You can view content of multiple files by entering more than one file name in command line, see the below example:

Linux Cat Command – Show Multiple File Content

Create New File: You be able to create new file by Cat command. Run the below command and then enter some strings and when it’s finished hit Enter, if you are end of a line and then Ctrl+D for save the new file. Please consider that existing files will be overwrite by run same command.

Linux Cat Command – Create New File

 

Join Multiple Files: Joining multiple files to a file is possible by Cat command. Here is an example:

# cat file1.txt file2.txt file3.txt > all-files.txt

Show Line Number: Cat command can show line number and content together. This is useful when you are reading files for debugging:

# cat -n sample1.txt
     1  This is frist line.
     2  This is second line
     3  This is third line.
     4  This is fourth line.
     5  This is fifth line.

Read more about “cat” at its man page.