How to Use ‘cat’ Command in Linux

How to Use 'cat' Command in Linux

The ‘cat’ command is a powerful tool in Linux that can be used to perform a variety of tasks related to managing and manipulating files. From displaying file contents to concatenating multiple files, the ‘cat’ command is an essential tool for any Linux user. In this comprehensive guide, we’ll explore the various options and syntax available for the ‘cat’ command, as well as tips and tricks for making the most of this versatile tool.


Syntax of the Cat Command

Before we dive into the various uses of the ‘cat’ command, let’s review the basic syntax. The ‘cat’ command follows the following structure:

cat [OPTION] [FILE]

Here, [OPTION] refers to the various options that can be used with the command, and [FILE] refers to the name of the file(s) that the command will operate on.


Displaying File Contents

The ‘cat’ command can be used to display the contents of a file on the terminal. To display the contents of a file named ‘example.txt’, you can use the following command:

cat example.txt

This will print the contents of the file on the terminal, as shown in the example output below:

$ cat example.txt
This is a sample text file.
It contains some text that we want to display using the 'cat' command.

Redirect Contents of File

The ‘cat’ command can also be used to redirect the contents of a file to another file. To redirect the contents of a file named ‘example.txt’ to a new file named ‘newfile.txt’, you can use the following command:

cat example.txt > newfile.txt

This will redirect the contents of the file ‘example.txt’ to ‘newfile.txt’, as shown in the example output below:

$ cat example.txt > newfile.txt
$ cat newfile.txt
This is a sample text file.
It contains some text that we want to display using the 'cat' command.

Print Line Numbers

The ‘cat’ command can also be used to print line numbers alongside the contents of a file. To display the contents of a file named ‘example.txt’ with line numbers, you can use the following command:

cat -n example.txt

This will display the contents of the file along with line numbers, as shown in the example output below:

$ cat -n example.txt
     1  This is a sample text file.
     2  It contains some text that we want to display using the 'cat' command.

Suppress Repeated Empty Lines

The ‘cat’ command can be used to suppress repeated empty lines in the output. To display the contents of a file named ‘example.txt’ with repeated empty lines suppressed, you can use the following command:

cat -s example.txt

This will display the contents of the file with repeated empty lines suppressed, as shown in the example output below:

$ cat -s example.txt
This is a sample text file.
It contains some text that we want to display using the 'cat' command.

Display TAB Characters

The ‘cat’ command can be used to display TAB characters in the output. To display the contents of a file named ‘example.txt’ with TAB characters displayed as ^I, you can use the following command:

cat -T example.txt

Display End of Lines

The ‘cat’ command can be used to display the end of lines in the output. To display the contents of a file named ‘example.txt’ with the end of lines displayed as $, you can use the following command:

cat -E example.txt

This will display the contents of the file with end of lines displayed as $, as shown in the example output below:

$ cat -E example.txt
This is a sample text file.$
It contains some text that we want to display using the 'cat' command.$

Concatenating Files

The ‘cat’ command can also be used to concatenate multiple files into a single file. To concatenate two files named ‘file1.txt’ and ‘file2.txt’ into a new file named ‘newfile.txt’, you can use the following command:

cat file1.txt file2.txt > newfile.txt

This will concatenate the contents of the two files into ‘newfile.txt’, as shown in the example output below:

$ cat file1.txt file2.txt > newfile.txt
$ cat newfile.txt
This is the contents of file1.txt
This is the contents of file2.txt

Creating Files

The ‘cat’ command can also be used to create new files. To create a new file named ‘newfile.txt’ with some text, you can use the following command:

cat > newfile.txt

This will create a new file named ‘newfile.txt’ and allow you to enter text. After entering the text, press ‘CTRL+D’ to save and exit the file, as shown in the example output below:

$ cat > newfile.txt
This is some text that will be written to the new file.

Conclusion

The ‘cat’ command is a versatile tool in Linux that can be used for a variety of tasks such as displaying file contents, redirecting file contents, printing line numbers, suppressing repeated empty lines, displaying TAB characters and end of lines, concatenating files, and creating new files. By understanding the various options and syntax available for the ‘cat’ command, you can increase your productivity and efficiency when working with files in Linux.

Got questions or suggestions on the article? Don’t hesitate to leave a comment below. Your feedback is valuable to us and helps improve the content quality. And don’t forget to share this article with others who might find it useful. Looking forward to hearing from you!

If our tutorials helped you, please consider buying us a coffee. We appreciate your support!

Thank you for your support.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top