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
catis 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
catto create a new file by redirecting its output.bash cat > newfile.txt -
Append to a file: You can add the output of
catto the end of an existing file using the append operator.bash cat additionalfile.txt >> existingfile.txt -
Display line numbers: The
-noption can be used to show line numbers alongside the file content.bash cat -n filename.txt -
Show non-printing characters: The
-voption 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
catdoes not reverse files directly, you can combine it withtacto achieve this.bash tac filename.txt -
Use
catwith pipes: You can usecatto send file contents to other commands via pipes.bash cat filename.txt | grep "search_term" -
Show file content with pagination: You can pipe
catoutput tolessfor easier viewing of large files.bash cat filename.txt | less