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

In this article, you’ll learn how to easily use the cat command and its examples in Linux.

$Cat [OPTION] [FILE]…

  1. Display file contents

The following example displays the /etc/passwd file.

# cat /etc/passwd

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
rumenz:x:500:500::/home/rumenz:/bin/bash
Copy the code

2. Terminal view the contents of multiple files In the following example, it will display files from the Rumenz and Rumenz1 terminals.

# cat rumenz rumenz1

Getting started at rumenz.com

We will create a file called rumenz2 using the following command to create a file.

# cat > rumenz2

Wait for user input, type the desired text, then press CTRL+D (hold CTRL Key and type D) to exit. The body will be written in the Rumenz2 file. You can view the contents of the file using the cat command.

# cat rumenz2

rumenz.com

  1. Cat with more and less arguments If there is a lot of content and the screen scrolls very quickly, we can use the arguments more and less

    cat song.txt | more

    cat song.txt | less

  2. Display the line number in the file with the -n option you can see the line number of a file song-.txt on the output side.

    cat -n song.txt

    1 Entry 2 Entry 3 Entry 4 Rumenz 5 Rumenz.com 6 Entry 7 Hello world

6. At the end of the file displayed below, you can see the -e option displayed at the end of the line, and also displayed in the space $if there is any gap between paragraphs. This option can be used to compress multiple rows into one.

# cat -e rumenz

rumenz$
rumenz.com$
rumenz com$
hello world$
Copy the code
  1. Displays TAB delimited lines in a file

  2. In the output below, we can see that the TAB space is filled with the ‘^I’ character.

    cat -T rumenz

    rumenz rumenz.com rumenz^Icom hello^Iworld

8. Display multiple files at a time

In the example below, we have three files rumenz, rumenz1, and rumenz2, and are able to view the contents of these files, as shown above. We need to separate each file; .

# cat rumenz; cat rumenz1; cat rumenz2

This is a rumenz file
This is the rumenz1 file.
This is rumenz2 file.
Copy the code
  1. Use standard output and redirection operators

We can redirect the standard output of a file to a new file that otherwise has the > (greater than) symbol in the existing file. Be careful, the existing content rumenz1 will be overwritten by the content rumenz file.

# cat rumenz > rumenz1

  1. Append standard output using the redirection operator

Appends the >> (double greater than) symbol to an existing file. The content here, the rumenz file, will be appended to the end of the file, the rumenz1 file.

# cat rumenz >> rumenz1

  1. Redirect standard input using the redirect operator

When you use redirection with standard input < (less than symbol), it uses the file name rumenz2 as the input and output of the command to be displayed on the terminal.

# cat < rumenz2

This is rumenz2 file.

  1. Redirects multiple files contained in a single file

This will create a file named rumenz3 and all output will be redirected to the newly created file.

# cat rumenz rumenz1 rumenz2 > rumenz3

  1. Sort the contents of multiple files in a single file

This will create a file rumenz4 and the output cat command will sort through the pipeline and the results will be redirected to the newly created file.

# cat rumenz rumenz1 rumenz2 rumenz3 | sort > rumenz4
Copy the code