Linux cat Command: How to Use it + Examples

The cat command is one of the most useful Linux commands you can learn. It derives its name from the word concatenate and let you create, merge or print files in the standard output screen or to another file and much more.

It does not require you to install anything since it comes pre-installed with the coreutils package in any Debian or Red Hat based system.

In this tutorial, we will cover the common usages of the Linux cat command explaining its features.

cat Command Syntax

Before we start exploring the article’s subject, we should log into the VPS using SSH, and quickly check the basic syntax. The command takes a filename as an argument along with options to specify particular operations.

cat [OPTION] [FILE]

To find all the available options, just type cat –help from the terminal.

cat Command Examples

Let’s check out some of the most common examples for using the cat command in Linux.

Using the cat Command to Create a File

Using the cat command you can quickly create a file and put text into it. To do that, use the > redirect operator to redirect the text in the file.

cat > filename.txt

The file is created, and you can begin populating it with text. To add multiple lines of text just press Enter at the end of each line.  Once you’re done, hit CTRL+D to exit the file.

To verify that the file is indeed created by the command used above, just use the following ls command in the terminal:

ls -l

Using the cat Command to View the Content of a File

This is one of the most basic usages of the cat command. Without any options, the command will read the contents of a file and display them in the console.

cat filename.txt

To prevent scrolling large files, you might want to add the option | more to output through the less or more display:

cat filename.txt | more

You can also display the content of more than one file. For example, to display content of all text files, use the following command in the terminal:

cat *.txt

A real-world usage for this cat command’s functionality is for outputting the content of the /etc/passwd file to list all users in a Linux system.

Using the cat Command to Redirect Content

Rather than displaying the contents of a file in the console you can redirect the output to another file using the option >. The command line would look like this:

cat source.txt > destination.txt

If the destination file does not exist then the command will create it, or overwrite an existing one by the same name.

To append the contents of the destination file, use the >> option along with the cat command:

cat source.txt >> destination.txt

Using the cat Command to Concatenate Files

This command also lets you concatenate multiple files into a single one. Basically it functions exactly like the redirection feature above, but with multiple source files.

cat source1.txt source2.txt > destination.txt

Like earlier, the above command will create the destination file if it does not exist, or overwrite an existing one with the same name.

Using the cat Command to Highlight Line Ends

The cat command can also mark line ends by displaying the $ character at the end of each line. To use this feature, use the -E option along with cat command:

cat -E filename.txt

Using the cat Command to Display Line Numbers

With the cat command you can also display the contents of a file along with line numbers at the beginning of each one. To use this feature, use the -n option with cat command:

cat -n filename.txt

Using the cat Command to Display Non-Printable Characters

To display all non-printable characters use the -v option along with cat command like in the following example:

cat -v filename.txt

To display tab characters only, use -T:

cat -T filename.txt

The tab characters will be shown as ^I

Using the cat Command to Suppress Empty Lines

To suppress repeated empty lines, and safe space on your display you can use the -s option. Keep in mind that this option will keep one blank line by removing the repeated empty lines only. The command would look like this:

cat -s filename.txt

Using the cat Command to Number Non-Empty Lines

To display non-empty lines with line numbers printed before them use the -b option. Remember the  -b option will override the -n option:

cat -b filename.txt

Using the cat Command to Display a File in Reverse Order

To view the contents of a file in reverse order, starting with the last line and ending with the first, just use the tac command, which is just cat in reverse:

tac filename.txt

Conclusion

That’s it. You now know all the basic features and functions of the cat command. You will now have the basic understanding to put it to good use. For more information on the cat command, you can always invoke manual page of cat with the command man cat !.

We hope this article helped you better your Linux Terminal skills. See you in the next one!

Linux cat Command FAQ

This section will answer the most frequently asked questions about the cat command.

How Does the cat Command Work?

The cat command reads files sequentially, displaying their content to the terminal. It concatenates multiple files if specified, allowing for viewing, combining, or redirecting output to another file. Use cat file1 file2 > newfile to merge or cat file to view a file’s content.

How Can I Display the Contents of a File Using the Cat Command?

To display a file’s contents, use cat filename. For example, cat example.txt shows the content of example.txt. For easier reading of large files, consider cat example.txt | less.

Can I Use Wildcards With the Cat Command?

Yes, cat supports wildcards. For example, cat *.txt it displays the contents of all .txt files in the current directory.

Author
The author

Edward S.

Edward is a content editor with years of experience in IT writing, marketing, and Linux system administration. His goal is to encourage readers to establish an impactful online presence. He also really loves dogs, guitars, and everything related to space.