Master the Linux ‘cat’ Command: A Comprehensive Guide

Peter Hou
2 min readMay 9, 2023

--

Get to grips with the ‘cat’ command in Linux, a versatile tool for handling text files.

Instructions

This article provides a comprehensive guide on how to use the ‘cat’ command in Linux to handle text files, making it ideal for Linux beginners.

History

The ‘cat’ command is part of the GNU core utilities package available on every Unix-like operating system. Its name comes from “concatenate,” indicating its primary purpose in the early stages of development.

When and why to use it

The ‘cat’ command is used to read, write, and concatenate files. It is also used to display the contents of a file, making it an essential tool for viewing and manipulating text files.

How to use it

Simply type ‘cat’ followed by the name of the file you want to display. For example:

cat filename.txt

The commonly used parameters

  • -n This parameter adds line numbers to the output.
cat -n filename.txt
  • -s This parameter squeezes multiple adjacent blank lines, causing the output to have no more than one blank line between each line of text.
cat -s filename.txt

Other supported parameters

  • -b Numbers non-blank output lines.
  • -E Displays a dollar sign at the end of each line, indicating the conclusion of that line.
  • -T Shows tab characters as ^I.

Most common use cases

  • Display the contents of a file:
cat filename.txt
  • Concatenate the contents of multiple files:
cat file1.txt file2.txt

The tricky skills

You can use ‘cat’ in conjunction with redirection operators to create a new file or overwrite the contents of an existing file. For example, the following command will create a new file named ‘newfile.txt’ containing the text ‘Hello, World!’:

cat > newfile.txt
Hello, World!

What needs to be noted

The ‘cat’ command does not require root privileges to run and is a safe command that will not modify your system. However, it is essential to remember that using ‘cat’ with redirection operators can overwrite existing files without warning.

Conclusion

The ‘cat’ command is an indispensable tool in Linux for managing and manipulating text files. Understanding its usage and parameters can aid in system administration and troubleshooting, particularly when dealing with text files.

--

--

Peter Hou
Peter Hou

Written by Peter Hou

I am a Senior Software Engineer and tech lead in a top tech company.

No responses yet