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:
cat [options] [file...]options: Command-line options to control the behavior ofcat.file: The file(s) to be read. If no file is specified,catreads from standard input.
Examples
Displaying File Contents
To display the contents of a file:
cat file.txtThis 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:
cat file1.txt file2.txtThis 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:
cat file1.txt file2.txt > combined.txtThis 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:
cat file1.txt >> file2.txtThis command appends the contents of file1.txt to the end of file2.txt.
Options
-n Option: Numbering Lines
To number all output lines:
cat -n file.txtThis command displays the content of file.txt with line numbers.
Example output:
1 First line
2 Second line
3 Third line-b Option: Numbering Non-Empty Lines
To number only non-empty lines:
cat -b file.txtThis command displays the content of file.txt with line numbers for non-empty lines only.
Example output:
1 First line
2 Second line
3 Third line-s Option: Squeeze Blank Lines
To suppress repeated empty lines:
cat -s file.txtThis 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:
cat -E file.txtThis command helps visualize the end of each line.
Example output:
First line$
Second line$
Third line$Practical Use Cases
Viewing File Contents
The most common use of cat is to quickly view the contents of a file.
cat /etc/passwdCreating Files
You can create a new file by using cat and redirecting input from the terminal:
cat > newfile.txtType 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:
cat part1.txt part2.txt part3.txt > complete.txtThis 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:
cat -n script.shSummary
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
Usage: cat [OPTION]... [FILE]...
Concatenate FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
-A, --show-all equivalent to -vET
-b, --number-nonblank number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends display $ at end of each line
-n, --number number all output lines
-s, --squeeze-blank suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version output version information and exit
Examples:
cat f - g Output f's contents, then standard input, then g's contents.
cat Copy standard input to standard output.man
NAME
cat - concatenate files and print on the standard output
SYNOPSIS
cat [OPTION]... [FILE]...
DESCRIPTION
Concatenate FILE(s) to standard output.
With no FILE, or when FILE is -, read standard input.
-A, --show-all
equivalent to -vET
-b, --number-nonblank
number nonempty output lines, overrides -n
-e equivalent to -vE
-E, --show-ends
display $ at end of each line
-n, --number
number all output lines
-s, --squeeze-blank
suppress repeated empty output lines
-t equivalent to -vT
-T, --show-tabs
display TAB characters as ^I
-u (ignored)
-v, --show-nonprinting
use ^ and M- notation, except for LFD and TAB
--help display this help and exit
--version
output version information and exit
EXAMPLES
cat f - g
Output f's contents, then standard input, then g's contents.
cat Copy standard input to standard output.Last updated