Cat command linux guide concatenate
Command definition
The cat
command in Linux is a versatile utility used for reading, displaying, and concatenating files. It allows users to view the contents of files directly in the terminal, combine multiple files into one, and create new files from existing ones. Additionally, cat
can be used to redirect output to other commands or files, making it a fundamental tool for file manipulation and viewing in Unix-like operating systems.
Usage examples
-
Display file contents: The most common use of
cat
is to display the contents of a file on the terminal.bash cat filename.txt
-
Concatenate multiple files: You can combine the contents of several files into one output stream.
bash cat file1.txt file2.txt
-
Create a new file: You can use
cat
to create a new file by redirecting its output.bash cat > newfile.txt
-
Append to a file: You can add the output of
cat
to the end of an existing file using the append operator.bash cat additionalfile.txt >> existingfile.txt
-
Display line numbers: The
-n
option can be used to show line numbers alongside the file content.bash cat -n filename.txt
-
Show non-printing characters: The
-v
option allows you to visualize non-printing characters in the output.bash cat -v filename.txt
-
Combine files and redirect output: You can concatenate multiple files and save the output to a new file.
bash cat file1.txt file2.txt > combined.txt
-
Display contents of a file in reverse order: While
cat
does not reverse files directly, you can combine it withtac
to achieve this.bash tac filename.txt
-
Use
cat
with pipes: You can usecat
to send file contents to other commands via pipes.bash cat filename.txt | grep "search_term"
-
Show file content with pagination: You can pipe
cat
output toless
for easier viewing of large files.bash cat filename.txt | less