Putorius
Basic Commands

cat Command - Reading and Combining Files in Linux

An image showing a Linux terminal with the command overlay cat command

The cat Linux command is used often by system administrators. Cat stands for concatenate and although that is it's main purpose, it is often used just to read or print files to standard output on the command line. In this short article we will discuss basic usage, using cat command with redirection operators and why you probably do not need to pipe it's output.

Basic Usage

In it's most simple usage, cat reads a file and prints it out to standard out.

$ cat /proc/cpuinfo 
processor : 0
vendor_id : GenuineIntel
cpu family : 6
model : 69
model name : Intel(R) Core(TM) i7-4600U CPU @ 2.10GHz
... OUTPUT TRUNCATED ...

You can use it to concatenate or print out multiple files.

$ cat file1 file2 file3
This is the 1st file
This is a second file
This is yet a third file

You can use the -n option do display line numbers in a file.

$ cat -n file4
1 This files has 6 lines
2 First line above, this is the second
3 this is the third line
4 this is the fourth ling
5 fifth line
6 sixth line

Use you can use the -e option to show the ends of lines notated with a $.

$ cat -e hello.txt 
Hello Putorius Readers,$
This is a basic text file that$
I am using to showcase the cat command.$
It is formatted weirdly for example this line has several odd spaces.$
$
$
$
$
$
$
Then I skipped a bunch of lines and continued down here.$

Use the -s option to squeeze or suppress repeated empty lines.

$ cat -s hello.txt 
Hello Putorius Readers,
This is a basic text file that
I am using to showcase the cat command.
It is formatted weirdly for example this line has several odd spaces.
Then I skipped a bunch of lines and continued down here.

The -T option to shows tabs notated with a ^I.

$ cat -T hello.txt 
Hello Putorius Readers,
^IThis is a basic text file that
I am using to showcase the cat command.
It is formatted weirdly for example this line has several odd spaces.
Then I skipped a bunch of lines and continued down here.

You can also use all of or any of these together.

$ cat -esT hello.txt 
Hello Putorius Readers,$
^IThis is a basic text file that$
I am using to showcase the cat command.$
It is formatted weirdly for example this line has several odd spaces.$
$
Then I skipped a bunch of lines and continued down here.$

Using cat Command with the Redirection Operators

You can use cat with the built in bash redirection operators as well.

Create a new file with the contents of an old file using standard output redirection.
NOTE: Using the > redirection will overwrite the target file if it exists.

cat file1 > newfile

You can append the contents of a file using the append redirection.

cat file2 >> newfile

You can concatenate multiple files into a single file easily.

$ cat file1 file2 file3 >> files
$ cat files
This is the 1st file
This is a second file
This is yet a third file.

If no source file is provided for redirection, cat will automatically redirect standard input (input from keyboard) into the target file.
NOTE: You must hit CTRL+D to exit.

$ cat >newfile

Gratuitous Piping cat Output

I see a lot of people who are new to Linux using the cat command and piping it's output to other commands. This is unnecessary as you can use the same arguments you use for cat directly with most other commands. Here are some example of misused cat commands.

Using cat piped to grep to find text in a file.

$ cat harrison-bergeron.txt  | grep gravity
Not only were the laws of the land abandoned, but the law of gravity and the laws of motion as well.
And then, neutraling gravity with love and pure will, they remained suspended in air inches below the ceiling, and they kissed each other for a long, long time.

This is a gratuitous use for cat, since you can get the same results with grep alone.

$ grep gravity harrison-bergeron.txt 
Not only were the laws of the land abandoned, but the law of gravity and the laws of motion as well.
And then, neutraling gravity with love and pure will, they remained suspended in air inches below the ceiling, and they kissed each other for a long, long time.

The same goes for piping cat output to sort.

$ cat file1st file2nd file3rd | sort -r
3rd file, only has one line
2nd file, only has one line
1st file, only has one line

You can achieve the same results with sort alone.

$ sort -r file1st file2nd file3rd
3rd file, only has one line
2nd file, only has one line
1st file, only has one line

There may be some legitimate use for piping the output of cat to another command. Feel free to discuss in the comments.

Conclusion

To learn more about pipelines and redirection operators, see Introduction to Linux IO, Standard Streams, and Redirection.

Cat is a very useful too, it can be used for everything from reading files from the command line to sorting and appending files with redirection. I am sure there are some things I missed, feel free to tell me in the comments.

PRO TIP: Need to read a file in reverse (from last line to first?) use the tac command.

Exit mobile version