Basic cat command in Linux with examples

In Linux operating systems, cat’ (short for “concatenate “) is one of the most widely used commands. It is a standard Unix application used to concatenate and display files. It has 3 related functions with regard to text files (1. Using the cat command we can see the file contents on a screen. Cat command concatenate FILE(s), or standard input, to preferred output. With no FILE, or when FILE is -, it reads trendy input. Also, you can use cat command for quickly growing a file. The cat command can read and write information from standard input and output devices.

This guide will teach you how to use various options of cat command. All the below examples are tested on RHEL/CENTOS 7.6

The Global Syntax of the cat command:

cat [OPTION]… [FILE]…

Mostly, everyone using the cat command to view the content of the file, so let’s begin with the same.

1. How to display the content of a file?

(Let’s say we have a file named linux.txt which contains few lines)

# cat linux.txt


Note: The very simple way to display the content of a file

2. How to use line numbers in a File?

(In the above file there are about 5 lines, let’s see how to use line number in the file)

# cat -n linux.txt

1 Train
2 Bus
3 Aeroplane
4 Ship
5 Car

Note: -n is the option to apply for line number

3. How to use number nonempty output lines in a file?

(For line number we have used the ‘-n’ parameter, whereas here we will be using ‘-b’, this is also similar to ‘-n’, but the difference is ‘-b’ will count only the non-blank lines (Means it does not calculate the empty/blank lines. In the below example I have added one space between Aeroplane and Ship))

# cat -n linux.txt                                                                                # cat -b linux.txt

1 Train                                                                                                      1 Train
2 Bus                                                                                                         2 Bus
3 Aeroplane                                                                                              3 Aeroplane
5 Ship                                                                                                       4 Ship
6 Car                                                                                                         5 Car

Note: The above command shows the difference between ‘-n and -b’ parameters of using the line numbers

4. How to display the content of a file per page?

(For eg: If we have a file which contains more than a page of content that won’t fit into the screen and it goes directly the last page of the file)

# cat linux.txt | more
# cat linux.txt | less

Note: Use ‘more and less’ combined with cat command to see the content per page. To combine the commands we need to use pipe (|) sign as above.

5. How view the multiple files contents together?

(Let’s say we have files named ‘linux.txt and teck.txt’. we need to see contents of the files together in a single command )

# cat linux.txt teck.txt
India              –          Delhi
Canada          –          Ottawa
Germany       –         Berlin
Malaysia        –         Kuala Lumpur
Japan             –         Tokyo

Note: The above command will display the contents of the two files together.

6. How to Sorting the contents of different files?

(Let’s say, files named ‘linux.txt and teck.txt’ having different contents line by line. The output content of the two files can be sorted )

# cat linux.txt teck.txt | sort
Canada            –          Ottawa
Germany         –          Berlin
India                –         Delhi
Japan              –         Tokyo
Malaysia         –          Kuala Lumpur

Note: Use ‘sort’ combined with cat command to see the sorted contents of the above files. To combine the command we need to use pipe (|) sign as above.

7. How to use redirect standard output?

(We can redirect the standard output contents into a new file or the existing with ‘>’ greater-than-symbol. If you choose the existing file then be careful, it will be overwritten.)

# cat linux.txt teck.txt | sort > testing.txt

Note: The above command combines the sorted content of ‘linux.txt and teck.txt’ into a new file named ‘testing.txt’. This command will check if the file exists or not. If not it will create as a new file else it will overwrite the new content.

8. How to avoid multiple blank spaces?

(We can squeeze multiple empty lines breaks in the file with one single empty line). Don’t confuse, the below example will show you what exactly it is:

I have a file named ‘teck.txt’ with some content, but there are big line breaks between the content of India and Canada. If we use a normal command ‘cat teck.txt’ it will display exactly as how the content is inside. But when you use the ‘-s’ parameter, it will avoid the big line breaks between the content of India and Canada with a single line break. It will not affect the content of the file, it will only display the content of the format. Just see the difference below :

Before After

# cat teck.txt

# cat -s teck.txt

Basic cat command in Linux with examples 2

Note: Just see above the difference of using the ‘-s’ parameter.

9. How to use show Tabs lines in a File?

(Using ‘-T’ parameter we could able to see the TAB space used in the content of the file, in the below example I have used a file named tabspace.txt, there I used TAB space between the words. For your easy understanding, I added ‘Before & After’ out here below:)

Before After

# cat tabspace.txt

# cat -T tabspace.txt

Basic cat command in Linux with examples 3

Note: The above example shows in the Before portion, I used single and double tabs between the words. The ‘After’ portion you could able to see the ‘exponent’ symbol with a capital ‘I’ (^I). Means for a sing tab you see (^I) and for double tab (^I^I).

10. How to show $ at the end of lines?

(Using ‘-E’ parameter will show ‘$’ sign at end of the each line, also if there is any blank space between the lines it shows the same ‘$’ sign. )

# cat -E teck.txt


India               –          Delhi$
Canada          –          Ottawa$
Germany       –          Berlin$
Malaysia        –         Kuala Lumpur$
Japan             –          Tokyo $

Note : The above output shows the ‘$’ sign ending at each line and between the blank space of each line.

11. How to create a new file using the cat command?

(Inlinux command mode there are many ways to create a new file, cat command is one amoung them)

# cat > newfile.txt
This is dummy file creation.
It is used for testing purpose.

Note : The above methord is used to create a new file using the cat command. Here I used ‘cat > newfile.txt’ it will create a file named ‘newfile.txt’, then you type the contents. Here I added two lines. After the 2nd line press enter and then use Ctrl+D to save the content. Thats it.!

12. How to append the existing file with cat command?

(Using ‘>>’ double greater than symbol to appends the existing file. Below I used a file named ‘newfile.txt’)

# cat >> newfile.txt
Just adding some contents in newfile.txt

Note : Here I used ‘newfile.txt’ the appended line will be added at the end of this file.

I hope this article will help you to learn the basic ‘cat’ commands in Linux !! Drop me your feedback/comment.

Thank you!

4 replies on “Basic cat command in Linux with examples”

You are welcome!, What are you actually trying to do? If you want to see the output result , then you can probably use tail -f. If not, could you please explain a little more in detail so that I can understand and give you the example in this regard.

Leave a Reply

Your email address will not be published. Required fields are marked *