The Cat command in Linux

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

In this article, please read Tipsmake to learn how to use the cat command useful with examples in Linux.

The general syntax of the Cat command in Linux

cat [OPTION] [FILE] .

Specific uses with the Cat command (with examples)

1. Display the contents of the file

In the example below, the command 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 Terminal

In the example below, the command will display the contents of the test and test1 files in Terminal.

# cat test test1 Hello everybody Hi world,

3. Create the file using the Cat command

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

# cat> test2

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

# cat test2 hello everyone, how do you do?

4. Use the Cat command with more and less options

If the file has a large amount of content that does not fit the output terminal and the screen scrolls up very quickly, you can use the more and less parameters with the cat command as follows:

# cat song.txt | more # cat song.txt | less

5. Display line numbers in file

With the -n option, you can see the line number of the song.txt file in the output terminal:

# cat -n song.txt 1 "Heal The World" 2 There's A Place In 3 Your Heart 4 And I Know That It Is Love 5 And This Place Could 6 Be Much 7 Brighter Than Tomorrow 8 And If You Really Try 9 You ' ll Find There's No Need 10 To Cry 11 In This Place You'll Feel 12 There's No Hurt Or Sorrow

6. Display $ at the end of file

In the section below, you can see that with the -e option, $ is displayed at the end of a line and in the space between paragraphs. This option is useful for squeezing multiple lines into one line.

# cat -e test hello everyone, how do you do? $ $ Hey, am fine. $ How's your training going on? $ $

7. Show tab-separated lines in the file

In the output below you can see the space TAB filled with the character ^ I.

# cat -T test hello ^ Ieveryone, how do you do? Hey, ^ Iam fine. ^ I ^ IHow's your training ^ Igoing on? Let's do ^ Isome practice in Linux.

8. Display multiple files at the same time

In the example below, there are 3 test files, test1 and test2. You can view the contents of those files as shown above. Remember to separate each file with semicolon.

# cat test; cat test1; cat test2 This is test file This is test1 file. This is test2 file.

9. Use standard output with the redirect operator

You can redirect the standard output of a file to a new file other than an existing file with the> symbol. Be careful, the existing content of test1 will be overwritten by the content of the test file.

# cat test> test1

10. Append the standard output with the redirect operator

Append the contents of the existing file with the >> symbol. Here, the content of the test file will be added to the end of the test1 file.

# cat test >> test1

11. Standard input redirection with redirect operator

When you use redirection with standard input <, the command uses the test2 file as input to a command and the output will be displayed in a terminal.

# cat 

12. Redirect multiple outputs to a single file

This will create a file called test 3 and all output will be redirected to the newly created file.

# cat test test1 test2> test3

13. Arrange the content of multiple files in a single file

This will create a test4 file and the output of the cat command will be sorted for sorting and the results will be redirected to a newly created file.

# cat test test1 test2 test3 | sort> test4

This article has covered the basic commands that can help you discover the cat command.

Wish you successful application!

4 ★ | 1 Vote