Linux cat command – With usage examples

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

It doesn’t require you to install anything, as it comes pre-installed with the coreutils package on any Debian or Red Hat based system.

In this tutorial, we will see the common uses of the Linux cat command by explaining its features.

Get up to 70% discount on your Linux VPS and accelerate your business growth with .

Linux cat command syntax

Before we start to delve into the topic of the article, we need to login to the VPS using and quickly check the basic syntax. The command takes a filename as an argument along with options to specify particular operations.

cat

To find all available options, simply type cat –help at the terminal.

Create a file with the cat command

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

cat > filename.txt

This creates the file and you can start filling it with text. To add multiple lines of text, simply press Enter at the end of each line. When you’re done, press CTRL + D to exit the file.

To verify that the file was indeed created by the above command, simply use the following ls command in the terminal:

ls -l

View the contents of a file with the cat command

This is one of the most basic uses of the cat command. Without the need for any options, the command will read the contents of a file and display it to the console.

See also  How to delete files and folders in Linux

cat filename.txt

To avoid scrolling through very large files, you can add the option | more see the screen less or more:

cat filename.txt | more

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

cat *.txt

Redirect content using the cat command

Instead of displaying the content of a file to 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, the command will either create it or overwrite an existing one with the same name.

To add the content of the destination file, use the option >> along with the cat command:

cat source.txt >> destination.txt

Concatenate files with the Linux cat command

This command also allows you to concatenate multiple files into one. In essence, it works exactly like the redirect function above, but with multiple source files.

cat source1.txt source2.txt > destination.txt

As before, the above command will either create the destination file if it doesn’t exist, or overwrite an existing one with the same name.

Mark the end of lines with the Cat command

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

cat -E filename.txt

Show line numbers with the cat command

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

See also  What is Dropshipping and how to get started?

cat -n filename.txt

Show non-printable characters with Cat command

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

cat -v filename.txt

To display only tab characters, use -T:

cat -T filename.txt

Tab characters will be displayed as ^I

Suppress empty lines with the Linux Cat command

To suppress repeated empty lines and save space on your screen, you can use the option -s. Note that this option will keep an empty line by removing only repeated empty lines. The command would look like this:

cat -s filename.txt

Numbering non-empty lines with the Cat command

To display non-empty lines with line numbers at the beginning of each line, use the option -b. Remember that the option -b will override the option -n:

cat -b filename.txt

Show a file in reverse order with the Cat command

To view the contents of a file in reverse order, starting with the last line and ending with the first, simply use the command tockWhat is it cat invested:

tock filename.txt

conclusion

That is all. You have learned 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 refer to the cat man page with the command man cat. You can also review others.

We hope this article has helped you improve your Linux Terminal skills. See you at the next one!

Gustavo is passionate about creating websites. He focuses on the application of SEO strategies at for Spain and Latin America, as well as the creation of high-level content. When he is not applying new WordPress tricks you can find him playing the guitar, traveling or taking an online course.

Loading Facebook Comments ...
Loading Disqus Comments ...