How to Use the Cat Command in Linux with 22 Practical Examples

Introduction

Linux commands are powerful tools that allow users to interact with the operating system, perform various tasks, and manipulate files and data. Among these commands, the “cat” command holds a special place for its versatility and usefulness. In this extensive guide, we will explore the “cat” command in Linux, break down its uses into simple terms, and provide you with 22 practical examples to help you become proficient in its usage.

1. What is the Cat Command?

The “cat” command in Linux stands for “concatenate.” It is a versatile utility that primarily deals with displaying and manipulating the contents of text files. The cat command can be used for various tasks, such as viewing file contents, combining files, creating new files, and more.

2. Basic Usage of Cat

To use the cat command, open your terminal or command prompt. The basic syntax of cat is as follows:

cat [options] [file(s)]
  • options: These are optional parameters that modify the behavior of the cat command.
  • file(s): These are the file(s) whose contents you want to display or manipulate. You can specify one or more files.

3. Displaying File Contents

The simplest and most common use of the cat command is to display the contents of a file. For example, to view the contents of a file named “example.txt,” use the following command:

cat example.txt

This command will display the entire content of “example.txt” in your terminal.

4. Concatenating Files

The cat command can also concatenate or combine the contents of multiple files and display them together. For instance, to concatenate two files, “file1.txt” and “file2.txt,” and display their combined content, use:

cat file1.txt file2.txt

This command will display the contents of both files as if they were a single file.

5. Creating New Files with Cat

You can use the cat command to create new files or overwrite the contents of existing ones. For example, to create a new file named “newfile.txt” and add some text to it, you can use:

cat > newfile.txt

After running this command, you can type the desired text, and when you’re done, press Ctrl+D to save the file.

6. Numbering Lines in a File

To number the lines in a file, you can use the -n option with cat. For example:

cat -n example.txt

This command will display the contents of “example.txt” with line numbers added to each line.

7. Displaying Line Numbers in a File

If you want to display line numbers in an existing file without modifying the file itself, you can use the “-n” option with the “-b” option. For example:

cat -n -b example.txt

This command will display the contents of “example.txt” with line numbers added only to non-blank lines.

8. Removing Blank Lines from a File

To remove blank lines from a file and display the contents without those lines, you can use the “cat” command in combination with the “grep” command. For instance:

cat example.txt | grep -v '^$'

This command will display the contents of “example.txt” while omitting any blank lines.

9. Appending to Existing Files

You can use the cat command to append the contents of one or more files to an existing file. For example, to append the contents of “file1.txt” and “file2.txt” to an existing file named “combined.txt,” use:

cat file1.txt file2.txt >> combined.txt

This command will add the contents of both files to “combined.txt” without overwriting its existing content.

10. Redirecting Output to a New File

If you want to save the output of a cat command to a new file, you can use the “>” symbol followed by the desired filename. For example, to create a new file named “output.txt” with the contents of “example.txt,” use:

cat example.txt > output.txt

This command will save the contents of “example.txt” in a new file named “output.txt.”

11. Using Cat with Pipelines

The cat command can be combined with pipelines to manipulate text data. For instance, you can use it to extract specific lines from a file and display them. For example, to display lines 5 to 10 from “example.txt,” use:

cat example.txt | sed -n '5,10p'

This command uses the “sed” command to extract and display the specified lines.

12. Viewing Non-Text Files

While cat is primarily used for text files, it can also be used to view the contents of non-text files, such as binary files. However, the output may not be human-readable, as binary files contain non-textual data. For example:

cat image.jpg

This command will display the binary data of the image file, which will not be meaningful to the user.

13. Counting Words, Lines, and Characters

To count the number of words, lines, and characters in a file, you can use the “wc” (word count) command in conjunction with cat. For example:

cat example.txt | wc

This command will provide a count of lines, words, and characters in “example.txt.”

14. Displaying Specific Lines

You can use cat to display specific lines from a file by specifying the line numbers with the “-n” option. For instance, to display lines 3 to 6 from “example.txt,” use:

cat -n example.txt | sed -n '3,6p'

This command will show lines 3 to 6 of the file.

15. Displaying the Beginning of a File

To display the beginning portion of a file, you can use the “head” command in combination with cat. For example, to view the first 10 lines of “example.txt,” use:

cat example.txt | head -n 10

This command will display the first 10 lines of the file.

16. Displaying the End of a File

Similarly, to display the end of a file, you can use the “tail” command with cat. For instance, to view the last 15 lines of “example.txt,” use:

cat example.txt | tail -n 15

This command will display the last 15 lines of the file.

17. Combining Multiple Files with Headers

To combine multiple files with headers that indicate the file names, you can use the cat command with the “-n” option to add line numbers. For example:

cat -n file1.txt && cat -n file2.txt

This command will display the contents of both files with line numbers and headers indicating the filenames.

18. Reversing File Contents

To reverse the order of lines in a file and display them in reverse, you can use the “tac” command in conjunction with cat. For example:

cat example.txt | tac

This command will display the contents of “example.txt” in reverse order.

19. Creating a File Backup with Cat

You can use the cat command to create a backup of a file by copying its contents to a new file. For instance, to create a backup of “example.txt” as “example_backup.txt,” use:

cat example.txt > example_backup.txt

This command will copy the contents of “example.txt” to a new file named “example_backup.txt.”

20. Extracting Specific Lines to a New File

To extract specific lines from a file and save them to a new file, you can use the “sed” command in combination with cat. For example, to extract lines 7 to 12 from “example.txt” and save them as “extracted_lines.txt,” use:

cat example.txt | sed -n '7,12p' > extracted_lines.txt

This command will create a new file, “extracted_lines.txt,” containing the specified lines.

21. Cat Command with Permissions and Ownership

When you use cat to create or overwrite a file, the new file inherits the permissions and ownership of the user running the command. It’s essential to be aware of the permissions and ownership of files, especially when manipulating system files.

22. Cat as a Debugging Tool

The cat command can be a helpful debugging tool for examining the contents of files during troubleshooting. You can use cat to view log files, configuration files, and other text-based data to identify issues and gather information for debugging purposes.

Conclusion

Congratulations! You’ve now become well-versed in the cat command and its diverse applications in Linux. The 22 practical examples provided in this guide empower you to efficiently manage text files, concatenate files, create new files, manipulate file contents, and perform various text-related tasks.

By mastering the cat command, you can streamline your file management tasks, extract valuable information, and troubleshoot issues effectively. Whether you’re a beginner or an experienced Linux user, the cat command remains a valuable tool in your arsenal.

In conclusion, the cat command in Linux simplifies text manipulation and provides you with the flexibility to work with text files effortlessly. Keep exploring and experimenting with the cat command to discover more ways to enhance your Linux experience. Happy cat-ing!

Leave a Comment