10 Linux Cat Command Examples for Beginners

If you a regular Linux command line user, I am sure you must have used the cat command. The tool is mostly used for displaying contents of a file, although it provides many other small but useful features. In this article, we will discuss the cat command in detail, explaining some of its key features.

Please note that all examples mentioned in this tutorial have been tested on Ubuntu 16.04 LTS.

Linux cat command

The tool's man page describes it as: 

cat - concatenate files and print on the standard output

Following are 10 points that'll give you a good idea about cat, including how it works and what features it provides:

1. How to view contents of a file using cat

The cat command allows you to view contents of a file on the standard output (stdout). This can be done in the following way:

$ cat [filename]

For example:

$ cat file1.txt

How to view contents of a file using cat

2. How to display multiple files using cat

The tool also allows you to display contents of multiple files in one go. This can be done in the following way:

$ cat [filename] [filename] ...

For example:

$ cat file1.txt  file2.txt

How to display multiple files using cat

3. How to display contents of file with line numbers

If you want, you can also display contents of a file with line numbers printed at the beginning of each line. This can be done by using the tool’s -n command line option.

$ cat -n [filename]

For example:

$ cat -n  file1.txt

How to display contents of file with line numbers

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. Here's how you do it:

$ 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

How to create file using cat command

To cross-verify the existence of the file as well as check what information it contains, I used the following command:

How to create file using cat command - result

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 > file3.txt

How to copy the contents of one file to another file

Please note that the output redirection operator (>) that we've used in this case flushes the destination file before the content is moved to it. So, it's worth knowing that there exists another redirection operator (>>) that just appends the contents to the destination file, instead of flushing it first.

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

How to make cat highlight line-ends

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 file6.txt

How to make cat suppress repeated empty lines

The first command in the above screenshot shows all the empty lines that are there in the file. But due to the -s command line option we used in the second cat command, the repeated empty lines got suppressed.

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 file7.txt

How to make cat display tab characters as ^I

As you can see in the above screenshot, the first time around TABs were displayed normally, but when the same command is executed with the -T option, TABs got replaced with ^I.

9. How to make cat display non printing characters

If you want, you can also make the cat command display non-printable characters. This can be done using the -v command line option. 

$ cat -v [filename]

For example:

$ cat -v file9.txt

How to make cat display non printing characters

As you can see in the above screenshot, the first command normally displayed contents of the file containing non-printable characters. But when we used the -v option, those characters were displayed in a special notation the option uses.

Note that LFD (line feeds) and TAB characters are exceptions for this command line option.

10. The -A option

In case, you need to use the -v, -E and -T option together, then instead of writing -vET in the command, you can just use the -A command line option.

$ cat -A  [filename]

For example:

$ cat -A file10.txt

The -A option

As you can see in the screenshot above, the first command shows the file (file10.txt) contains two lines. Those lines contain tabs as well as some non-printable characters. So, when the same command was run with the -A command line option, tabs got replaced by ^I, and non-printing characters were displayed in a special notation. And finally, each line ended with a $.

So, effectively, -A did what -vET would have done.

Conclusion

We've covered most of the cat command options here, so practicing them should give you a pretty good idea about the tool. Do try them out, and once done, head over to the command's man page to learn more about it. 

Share this page:

Suggested articles

1 Comment(s)

Add comment

Comments

From: Pete at: 2017-05-17 14:50:43

Some useful options above.

99.9999% of the time when I see someone using 'cat', it isn't necessary.  Pretty much every command that cat is used to create a stdin stream would be faster, less resources used, if the next command just read the file directly.  Books and online examples commonly show

$ cat file | grep something

This is wasteful.  grep knows how to read a file directly. grep knows how to read multiple files using globbing directly too.

Also, using 'cat' to display a file is usually not as useful as using more/less instead.

Of course, there are times when cat **is** useful and desired.  I run into those about once a year.