The cat (concatenate) command is used to view, create, and concatenate files in Linux. It is one of the most commonly used commands for working with text files.
cat [OPTIONS] [FILE...]cat file.txtπΉ This prints the content of file.txt to the terminal.
cat file1.txt file2.txtπΉ This concatenates and displays both files.
cat > newfile.txtπΉ This allows you to type text into newfile.txt.
πΉ Press Ctrl + D to save and exit.
cat >> existingfile.txtπΉ This appends new content to existingfile.txt.
πΉ Press Ctrl + D to save and exit.
cat file1.txt > file2.txtπΉ This overwrites file2.txt with the content of file1.txt.
cat file1.txt file2.txt > merged.txtπΉ This combines file1.txt and file2.txt into merged.txt.
cat -n file.txtπΉ This displays the file with line numbers.
cat -s file.txtπΉ This suppresses consecutive empty lines.
cat largefile.txt | lessπΉ This allows scrolling through the file using less.
tac file.txtπΉ tac is like cat, but displays lines in reverse order.
πΉ Combine all .log files into one file
cat *.log > all_logs.txtπΉ Check the contents of a system log file
cat /var/log/syslog | grep "error"πΉ Copy a file while keeping permissions intact
cat source.txt | tee destination.txt| Command | Description |
|---|---|
cat file.txt |
Show contents of file.txt |
cat > file.txt |
Create a file and write to it |
cat >> file.txt |
Append to an existing file |
cat file1 file2 |
Concatenate files |
cat -n file.txt |
Show line numbers |
cat -s file.txt |
Remove extra blank lines |
π₯ Pro Tip: Use less or more instead of cat for very large files to avoid terminal overflow.
less largefile.txt