cat
The cat
command (short for concatenate) is one of the most fundamental and frequently used utilities in Unix-like systems. It is primarily designed to display the contents of files, concatenate multiple files, and send the output to standard output (typically your terminal). Although its functionality is simple, cat
is a versatile tool that can be combined with other commands in pipelines and scripts to streamline text processing tasks. This comprehensive guide covers the syntax, options, practical examples, advanced tips, and further reading for mastering the cat
command.
Table of Contents
Introduction
The cat
command is an essential tool for anyone working on Unix/Linux systems. It is most commonly used to display the contents of a file directly in the terminal. Beyond that, cat
can join multiple files into one output stream, create new files, and even help in debugging scripts by quickly showing file content. Its simplicity makes it a favorite for quick operations, while its compatibility with pipes allows it to serve as a building block in more complex command-line workflows.
Basic Syntax and How cat
Works
The general syntax of the cat
command is:
FILE...: One or more files to display or concatenate. If no file is specified,
cat
reads from standard input.OPTIONS: Modify the behavior of
cat
(e.g., numbering lines, showing non-printing characters, etc.).
Example:
This command displays the contents of file1.txt
on the terminal.
Common Options and Parameters
While cat
is simple by nature, several options can enhance its functionality:
Numbering Lines (-n
and -b
)
-n
or--number
:
Number all output lines.cat -n file.txtThis displays the content of
file.txt
with each line prefixed by its corresponding line number.-b
or--number-nonblank
:
Number only non-blank lines.cat -b file.txtThis option is useful when you want to avoid numbering blank lines.
Displaying End-of-Line Characters (-E
)
-E
or--show-ends
:
Display a dollar sign ($
) at the end of each line to make end-of-line characters visible.cat -E file.txt
Squeezing Blank Lines (-s
)
-s
or--squeeze-blank
:
Replace multiple adjacent blank lines with a single blank line.cat -s file.txtThis option is helpful for cleaning up output from files with excessive spacing.
Showing Non-Printing Characters (-v
)
-v
or--show-nonprinting
:
Use this option to display non-printing characters (except for tabs and the end-of-line) in a visible form. Often combined with-E
and-T
for a complete view.cat -v file.txt
Practical Examples
Viewing File Contents
Simply display the contents of a file:
If you have a long file and want to see line numbers:
Concatenating Multiple Files
You can join multiple files and display their combined content:
To concatenate files and save the result to a new file:
Using cat
in Pipelines
cat
is often used to feed file contents into other commands. For example, to count the number of lines in a file:
Or to view a file with syntax highlighting using another tool (like less
):
Creating and Appending Files
You can use cat
to create new files. For example, to create a file named newfile.txt
:
Then type your content and press Ctrl+D to save the file.
To append content to an existing file:
Again, type the new content and press Ctrl+D when finished.
Advanced Usage and Tips
Combining Options:
You can combine multiple options to tailor the output. For instance, to number non-blank lines and squeeze multiple blank lines:cat -b -s file.txtUsing with Other Tools:
Althoughcat
is simple, it is frequently used with other commands likegrep
,sed
,awk
, andwc
in complex pipelines to process text.cat file.txt | grep "pattern"Avoiding Useless Use of
cat
:
Whilecat
is very handy, sometimes it’s not necessary. For example, instead of writing:cat file.txt | wc -lYou could simply use:
wc -l file.txtThis is known as the “useless use of cat” and avoiding it can lead to more efficient scripts.
Viewing Hidden Characters:
Combining-v
,-E
, and-T
(to show tabs as^I
) can help diagnose issues in files where whitespace is significant.cat -vET file.txtBinary Files:
Althoughcat
can display binary files, be cautious because non-text files may produce garbled output or affect your terminal settings.
Conclusion and Further Reading
The cat
command is an indispensable tool for Unix/Linux users, providing a simple and efficient way to view, concatenate, and create files. Its versatility and ease of use make it a core component of many shell scripts and command-line workflows. Whether you are a beginner or an advanced user, understanding cat
and its various options can significantly streamline your text-processing tasks.
Further Reading and Resources
Manual Page:
To view the complete documentation, type:man catOnline Documentation:
Tutorials and Community Discussions:
Websites such as Stack Overflow and various Unix/Linux blogs offer additional examples and creative uses ofcat
.
Experiment with cat
on your own files and in your pipelines to discover how this simple tool can greatly enhance your productivity. Happy concatenating!