13 Basic Cat Command Examples in Linux Terminal – Tecmint

The cat command (short for “concatenate“) is one of the most commonly used commands in Linux/Unix-like operating systems. The cat command allows us to create one or more files, view the contents of a file, concatenate files and redirect the output in terminal or files.

In this article, we will discover the practical use of cat commands with their examples in Linux.

Also read: Learn how to use ‘cat’ and ‘tac’ (reverse of the cat command) in Linux

General syntax of the cat

$ cat command [OPTION] [FILE]… 1. Display the contents of the file

The following example will display the contents of the /etc/passwd file. # cat /etc/passwd

root:x:0:0:root:/root:/bin/bash bin:x:1:1:bin:/bin:/sbin/nologin narad:x:500:500::/home/narad:/bin/bash

2. View the contents of multiple files

in the terminal

In the following example, it will display the contents of the test and test1 file in the terminal

. # cat test1 Hello everyone Hello world,

3. Create

a file with the cat command

We will create a file called test2 file with the following command

. # cat >test2

Wait for user input, type the desired text, and press CTRL+D (hold down the Ctrl key and type ‘d‘) to exit. The text will be written to the test2 file. You can view the contents of the file with the following cat command.

# Cat test2 Hello everyone, how do you do it?

4. Use

the cat command with more and less options

If a file that has a lot of content that does not fit in the output terminal and the screen scrolls very fast, we can use plus and minus parameters with the cat command as shown below

. # Cat Song.txt | More # Cat Song.txt | Minus

5. Show

line numbers in the file With the

-n option you can view the line numbers of a stock song.txt in the output terminal.

# cat -n song.txt 1 “Heal The World” 2 There is a place in 3 Your heart 4 And I know it is love 5 And this place could 6 be much 7 brighter than tomorrow 8 And if you really try 9 you will find that there is no need for 10 to Cry 11 In this place you will feel 12 There is no pain or sadness

6. Show

$ at the end of the file

You can then see with the -e option that ‘$’ is displayed at the end of the line and also in the space showing ‘$‘ if there is any space between paragraphs. This option is useful for compressing multiple lines into a single line.

# cat -e test hello everyone, how’s it going?$ $ Hey, I’m fine.$ How is your training going?$

$7. Show tab-separated lines in

the

file In the next output, we could see that the TAB space is filled with the characters ‘^I’.

# cat -T try hello ^I everyone, how do you do it? Hey, ^I’mfine. ^ I^IHow’s your training ^Igoing on? Let’s ^Ido some practice on Linux.

8. Show multiple files at once

In the following example we have three test files, test1 and test2, and we can see the contents of those files as shown above. We need to separate each file with ; (semicolon).

# cat test; cat test1; cat test2 This is a test file This is the test1 file. This is the test2 file. 9. Use standard output with

redirection operator We can redirect

the standard output of a file to a new file or existing file with a ‘>‘ symbol (greater than). The careful and existing content of the test1 will be overwritten with the contents of the test file.

# Cat test > test1

10. Append standard output with redirection operator

Appended to existing file with symbol ‘>>‘ (twice greater than). Here, the contents of the test file will be appended to the end of the test1 file.

# Cat test >> test1 11

. Standard input

redirection with redirection operator When you use

the redirection with standard input ‘<‘ (less than symbol), you use the filename test2 as input for the command and the output will be displayed in a terminal. # cat < test2 This is the test2 file. 12.

Redirect multiple files contained

in a single file

This will create a file called test3 and all output will be redirected into a newly created file

. # Cat test1 test2 > test3 13

. Sort the contents of multiple files into a single

file

This will create a test4 file and the output of the cat command will be piped to sort and the result will be redirected to a newly created file

. # cat test1 test2 test3 | sort > test4

This article lists basic commands that can help you explore cat commands. You can refer to the cat command man page for more options.

In our next article, we will cover more advanced cat commands. Please share it if you find this article useful via our comment box below.