12 basic cat command in Linux with examples

'Cat' is one of the most commonly used commands in Linux operating systems (short for "concatenate"). This is a standard Unix application that concatenates and displays files. There are three functions associated with text files (1. By using the cat command, we can see the contents of a file on screen. Cat concatenates FILE(s) or standard input, to a preferred output. With no FILE, or when FILE is -, it reads trendy input. It is also possible to grow files quickly using cat commands. Using the cat command, you can read and write data from standard input and output devices.

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

Global Syntax of the cat command:

cat [OPTION]... [FILE]...

Mostly, everyone is 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 a few lines)

# cat linux.txt

Train
Bus
Aeroplane
Ship
Car

Note: A 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 of 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
4
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 that contains more than one page of content that won't fit into the screen and it goes directly to the last page of the file)

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

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

5. How do you view the multiple files content together?

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

# cat linux.txt teck.txt
Train
Bus
Aeroplane
Ship
Car
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 sort the contents of different files?

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

# cat linux.txt teck.txt | sort
Aeroplane
Bus
Canada            -          Ottawa
Car
Germany         -          Berlin
India                -         Delhi
Japan              -         Tokyo
Malaysia         -          Kuala Lumpur
Ship
Train

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 it is not created as a new file else it will overwrite the new content.

8. How to avoid multiple blank spaces?

(We can squeeze multiple empty line breaks into the file with one single empty line). Don't be confused, 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

12 basic cat command in Linux with examples 1

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

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

(Using the '-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 ease of understanding, I added 'Before & After' out here below:)

Before After

# cat tabspace.txt

# cat -T tabspace.txt

12 basic cat command in Linux with examples 2

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 a double tab (^I^I).

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

(Using '-E' parameter will show '$' sign at end of 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?

(In Linux command mode there are many ways to create a new file, cat command is one among them)

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

Note : The above method 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. That's it.!

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

(Using '>>' double greater than the symbol to append 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/comments.

Thank you!

4 replies on “12 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 *

L