Linux Tutorial
Linux File/Directory Management
Linux Packaging And Compression
Vim Text Editor
Linux Text Processing
Linux Software Installation
Linux User/User Group Management
Linux Permission Management
Linux Filesystem Management
Linux Advanced Filesystem Management
Linux System Management
Linux Backup and Recovery
Linux System Service Management
Linux System Log Management
Linux Boot Management
LAMP/LNMP Environment
SELinux Management
The cat
command in Linux (short for "concatenate") is a utility used to read, display, and concatenate the contents of files. In this tutorial, we will discuss how to use the cat
command effectively, along with various options and examples.
Basic usage of the cat
command:
To display the contents of a file, use the cat
command followed by the name of the file:
cat file.txt
This command will display the contents of file.txt
in the terminal.
Displaying multiple files:
You can display the contents of multiple files by providing multiple file names:
cat file1.txt file2.txt file3.txt
This command will display the contents of file1.txt
, file2.txt
, and file3.txt
one after the other.
Concatenating files:
The cat
command can be used to concatenate multiple files into a single file. To do this, provide multiple input file names and use a redirection operator to specify the output file:
cat file1.txt file2.txt > output.txt
This command will concatenate the contents of file1.txt
and file2.txt
into a new file named output.txt
. If output.txt
already exists, its contents will be overwritten.
Appending files:
If you want to append the contents of one or more files to an existing file, use the >>
redirection operator:
cat file1.txt >> output.txt
This command will append the contents of file1.txt
to output.txt
. If output.txt
does not exist, it will be created.
Displaying line numbers:
To display line numbers along with the file contents, use the -n
or --number
option:
cat -n file.txt
This command will display the contents of file.txt
with line numbers.
Squeezing blank lines:
If a file has multiple consecutive blank lines, you can use the -s
or --squeeze-blank
option to reduce them to a single blank line:
cat -s file.txt
This command will display the contents of file.txt
with consecutive blank lines squeezed into a single blank line.
Displaying non-printable characters:
To display non-printable and whitespace characters, use the -v
, -t
, and -e
options:
-v
or --show-nonprinting
: Displays non-printable characters.-t
or --show-tabs
: Displays TAB characters as ^I
.-e
: Appends a $
character to the end of each line, making it easier to see line breaks.cat -vte file.txt
This command will display the contents of file.txt
with non-printable characters, TAB characters, and line breaks visible.
By following this tutorial, you should now have a good understanding of how to use the cat
command in Linux for reading, displaying, and concatenating files. With various options and the ability to handle multiple files, the cat
command is a versatile tool for managing file contents in Linux.
How to use cat
command in Linux:
cat
command.cat file.txt
Concatenating files with cat
in Linux:
cat file1.txt file2.txt > combined.txt
Print multiple files with cat
command:
cat file1.txt file2.txt
Appending files with cat
in Linux:
cat file1.txt >> file2.txt
Redirecting cat
output to a file:
cat
to a new file.cat file.txt > newfile.txt
Displaying line numbers with cat
:
cat -n file.txt
Using cat
to create and view text files:
cat
.cat > newfile.txt
Concatenating non-text files with cat
:
cat image1.jpg image2.jpg > combined.jpg
Removing duplicate lines with cat
:
cat
with sort
to remove duplicate lines in a file.cat file.txt | sort -u
Concatenate and create new files with cat
command:
cat
.cat file1.txt file2.txt > newfile.txt