Skip to content

11 Practical Example of cat Command in Linux

Advertisement

The cat command in Linux is a simple yet powerful tool that is used to view and manipulate text files. It is short for “concatenate,” which means to combine or link together. The cat command has a variety of uses, from displaying the contents of a file to combining multiple files into a single file.

In this article, we’ll cover 11 practical examples of the cat command in Linux, so you can get the most out of this versatile tool.

Syntax

The syntax for the cat command in Linux is as follows:

Where:

  • [OPTION] is an optional argument that modifies the behavior of cat. Some common options include:
    • -n: displays line numbers
    • -v: displays non-printable characters
    • -e: displays end-of-line characters as $
    • -T: displays tabs as ^I
  • [FILE] is the name of the file you want to view or manipulate. You can list multiple files to concatenate them, or you can use a - to read from standard input.

11 Practical Examples of Linux `cat` Command

Here are the 11 practical examples of the Linux cat command.

  1. Display the contents of a file
  2. The most basic use of cat is to display the contents of a single file. To do this, simply type cat followed by the name of the file:

    cat file.txt 
    

  3. Concatenate multiple files into a single file
  4. You can also use cat to combine the contents of two or more files into a single file. To do this, simply list the names of the files you want to combine, separated by spaces:

    cat file1.txt file2.txt > combined.txt 
    

  5. Display the contents of multiple files
  6. If you want to display the contents of multiple files at once, you can simply list the names of the files after the cat command:

    cat file1.txt file2.txt 
    

  7. Display line numbers
  8. By default, cat does not display line numbers. However, you can use the -n option to display line numbers:

    cat -n file.txt 
    

  9. Display non-printable characters
  10. By default, cat does not display non-printable characters, such as control characters or whitespace. However, you can use the -v option to display these characters:

    cat -v file.txt 
    

  11. Display end of line characters
  12. If you want to see the end of line characters in a file, you can use the -e option:

    cat -e file.txt 
    

  13. Display tabs as ^I
  14. To display tabs in a file as ^I, you can use the -T option:

    cat -T file.txt 
    

  15. Display only specific lines
  16. You can use cat to display only specific lines from a file. To do this, you’ll need to use the head and tail commands in combination with the cat. For example, to display the first 10 lines of a file:

    head -n 10 file.txt | cat 
    

  17. Copy the contents of a file
  18. You can use cat to copy the contents of a file to another file. To do this, simply use the > operator to redirect the output of cat to a new file:

    cat file1.txt > file2.txt 
    

  19. Append to an existing file
  20. To add new content to an existing file, you can use the >> operator to append the output of cat to the end of the file:

    cat new_content.txt >> file.txt 
    

  21. Combine files using pipes
  22. Finally, you can also use pipes to combine the output of multiple cat commands. For example, to combine the contents of two files and then display the result:

    cat file1.txt file2.txt | cat 
    

These are just a few of the many ways you can use the cat command in Linux.

Conclusion

In conclusion, the cat command is a simple yet powerful tool that is an essential part of any Linux user’s toolkit. Whether you’re looking to quickly view the contents of a file, combine multiple files, or copy files from one location to another, cat is a versatile and powerful tool that’s worth mastering. By understanding and utilizing the 11 practical examples outlined in this article, you’ll be well on your way to becoming a pro at using the cat command in Linux.

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply

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


Back to top