cat
cat
The cat
command in Unix and Linux is used to concatenate and display the content of files. It's one of the most frequently used commands for viewing file contents, combining files, and redirecting output.
Basic Usage
The basic syntax for the cat
command is:
options
: Command-line options to control the behavior ofcat
.file
: The file(s) to be read. If no file is specified,cat
reads from standard input.
Examples
Displaying File Contents
To display the contents of a file:
This command outputs the entire content of file.txt
to the standard output (usually the terminal).
Concatenating Files
To concatenate multiple files and display their contents:
This command displays the contents of file1.txt
followed by file2.txt
.
Redirecting Output to a File
To concatenate files and redirect the output to a new file:
This command combines file1.txt
and file2.txt
into a new file named combined.txt
.
Appending to a File
To append the contents of one file to another:
This command appends the contents of file1.txt
to the end of file2.txt
.
Options
-n
Option: Numbering Lines
To number all output lines:
This command displays the content of file.txt
with line numbers.
Example output:
-b
Option: Numbering Non-Empty Lines
To number only non-empty lines:
This command displays the content of file.txt
with line numbers for non-empty lines only.
Example output:
-s
Option: Squeeze Blank Lines
To suppress repeated empty lines:
This command reduces multiple consecutive blank lines to a single blank line.
-E
Option: Displaying End-of-Line Characters
To display a $
at the end of each line:
This command helps visualize the end of each line.
Example output:
Practical Use Cases
Viewing File Contents
The most common use of cat
is to quickly view the contents of a file.
Creating Files
You can create a new file by using cat
and redirecting input from the terminal:
Type the content you want to add to newfile.txt
and press Ctrl+D
to save and exit.
Combining Files
cat
is useful for combining multiple files into one:
This combines part1.txt
, part2.txt
, and part3.txt
into complete.txt
.
Displaying Line Numbers
To help debug scripts or code, you can display line numbers:
Summary
The cat
command is a simple yet powerful tool for viewing, combining, and manipulating file content in Unix and Linux environments. Its versatility makes it an essential command for file management and text processing tasks.
help
man
Last updated