10 Essential Cat Command Examples for Linux Users

Published on December 26, 2023

According to its man page, the cat command concatenates files and displays them on the standard output. Provided you run and use Linux, you can’t miss running into the cat command at some point. Therefore, understanding the different commands you can use with it for different situations will save time. The cat command helps you quickly work with files, and this post presents 10 essential cat command examples you should know to help you understand how to use it. Let’s begin!

Different Cat Command Examples

For each command, we give an example of how to apply it. Go through each and try it on your end to get comfortable working with the cat command.

1) Creating a File

Although Linux has different ways to create files, the cat command is another option. Whether you wish to create the file in your current directory or specify a different path, using it to create a file is straightforward.

Follow the syntax below.

Copy

Once you execute the create command, you can enter the text you want to store inside the file. Once you have all the text you want to store, press Ctrl + D to save the file.

For this example, we’ve created file1.txt. You must include the greater than (>) sign in your command. Otherwise, your file won’t get created.

When we run the ls command, we verify that we successfully created the file.

That’s our first example of using the cat command in Linux.

2) Read File Contents

After creating your file using the cat command, you can check its contents using the same command. For that, we will run a command similar to what we did in the previous example, but we won’t include the greater than symbol.

Our command would be:

Copy

You will get an output printing the file contents on the standard output.

Suppose you want to read the contents of numerous files. You can still run the cat command and specify all the target files you want to read their contents.

For this example, we are opening the contents of two files. Their contents will be combined and displayed in the standard output.

3) Add Line Numbers

So far, you might have noticed that when we displayed the contents of the files, we didn’t get the output arranged in a number format. However, we can add line numbers for each line in our output. For that, we include the -n option.

Here’s an example.

Copy

4) Append Text to a File

With the cat command, you can append text to an existing file. For that, we use double greater than signs. When the >> sign is spotted, it indicates that the current contents in the file will be retained and the new ones you add will be appended at the bottom.

Taking the below names.txt file, let’s see how to append it.

Run the command below and use the same file name.

Copy

Next, add the new content you want to append to it.

Lastly, use the cat command to display its contents. The output below verifies that we managed to append the file and now have more content.

5) Copy File(s) Content

Sometimes, you may want to create a duplicate of a file. Maybe you want to have another file containing the same content in an existing file. With the cat command, you can copy a file’s content and have it in another.

We will use the syntax below.

Copy

We confirm that a new file was created, and when we run the cat command to read its contents, it contains the same contents as the primary file we have.

Using the same logic, you can combine numerous files and have them as the contents of one file. For that, run the cat command, specify the files, and add the new file you wish to have, as demonstrated in the example below.

Copy

You will get a combined file as the output.

6) View Head and Tail

When viewing the contents of a file, you can access the head and tall sections. The head contains the first lines at the top of the file, while the tail is the last line.

To access the head, here’s an example of displaying the first four lines in our file.

Copy

To implement the same for the last lines, we will run the tail command and specify the number of lines.

Copy

7) Use with Grep

You can use the cat command with the grep command to filter the displayed contents of your target file. Here, we’ve added the grep command to filter where “linux” is found in the displayed contents.

Copy

8) Reverse Order

When displaying the contents of a file, you can reverse the order of the contents to start printing them starting from the bottom line.

Suppose we have the names.txt file containing the following lines; we can reverse the order.

Copy

To reverse the order, we will use the tac command, which is the reverse of the cat command.

Copy

9) Show Line Endings

For a file with multiple long lines, having a way to show the end of the line is useful. With the -E option, you will get the $ symbol at the end of each line.

Copy

10) View in Binary Format

You can display the contents of a file in binary format. To convert the output, pipe the file’s contents using the xxd -b option, and you will get a binary output.

Copy

Conclusion

The cat command is useful in Linux for concatenating files. This post shared 10 practical cat command examples to make you comfortable. Hopefully, this post has been beneficial to your understanding of the cat command.

New to LinuxMeta? Get Started Now! 

Instantly Deploy Linux & Windows KVM VPS at a Cheap Price