What is the cat command in Linux?

The cat command is one of the Linux shell commands that the operating system kernel provides. It reads data from files and displays it on the console. It comes from the word 'concatenate,' as we can use this command to display the contents of different files together.

Let's suppose we have to read data from multiple files and merge them into one file. How can we do this?

We do this primarily with the help of the cat command, in which we read data from multiple files. The content in each file would be concatenated with the already read content of the previous file. We can then send all the concatenated data to the new file using pipes.

Syntax

Below we can see the syntax for the cat command:

cat [Options] [Files]
cat command syntax

In the command, we see two placeholders:

  • Files: Here, we mention the file path whose content we want to display or concatenate. If we have multiple files, we separate them with a space.

  • Options: This represents the various options we can use to modify the behavior of the cat command. We can see the various options we can use with the command in the table below.

Options for cat

Option

Description

-A / --show-all

Shows all content in the file including hidden content.

-b / --number-nonblank

Only numbers nonempty lines. Used with -n option.

-E / --show-ends

Display a $ sign at the end of each line.

-n / --number

Output line numbers along with lines.

-s / --squeeze-blank

Replaces consecutive empty lines with a single empty line.

-t

Displays TAB characters (^I). Used with -v option.

-u

Does not buffer output.

-v / --show-nonprinting

Displays nonprinting characters such as ASCII control characters.

Example

Now that we have reviewed the syntax, let's look at an example to see how the command works. For the purpose of this example, we will use two text files, file1.txt and file2.txt.

Below, we can see the content of both text files.

Hello Educative User!
Lorem ipsum dolor sit amet, consectetur adipiscing elit
sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.
Content in file1.txt
How Are You?
Content in file2.txt

In this example, we will concatenate the content in file1.txt and file2.txt. When we click on the terminal, the following command runs to display the concatenated content of both text files.

cat file1.txt file2.txt
Command to concatenate files
Terminal 1
Terminal
Loading...

Challenge: In the terminal above, try to execute the cat command with the options shown in the table above and observe how the command behaves.

Exercise

Now let's try to solve the exercise below by matching the option to their correct description.

Match The Answer
Select an option from the left-hand side

-n

Display line numbers in the output.

-b

Display non-printing characters, such as tabs and newlines.

-E

Display non-empty lines with line numbers in the output.

-v

Display the end of each line with the $ character.


Free Resources

Copyright ©2024 Educative, Inc. All rights reserved