Skip to content

Latest commit

Β 

History

History
115 lines (90 loc) Β· 2.54 KB

File metadata and controls

115 lines (90 loc) Β· 2.54 KB

🐱 cat Command in Linux

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.


πŸ“Œ Basic Syntax

cat [OPTIONS] [FILE...]

πŸ”Ή Common Uses of cat

1️⃣ Display the Contents of a File

cat file.txt

πŸ”Ή This prints the content of file.txt to the terminal.

2️⃣ View Multiple Files

cat file1.txt file2.txt

πŸ”Ή This concatenates and displays both files.

3️⃣ Create a New File & Write to It

cat > newfile.txt

πŸ”Ή This allows you to type text into newfile.txt.
πŸ”Ή Press Ctrl + D to save and exit.

4️⃣ Append Text to a File

cat >> existingfile.txt

πŸ”Ή This appends new content to existingfile.txt.
πŸ”Ή Press Ctrl + D to save and exit.

5️⃣ Copy Contents from One File to Another

cat file1.txt > file2.txt

πŸ”Ή This overwrites file2.txt with the content of file1.txt.

6️⃣ Merge Multiple Files into One

cat file1.txt file2.txt > merged.txt

πŸ”Ή This combines file1.txt and file2.txt into merged.txt.

7️⃣ Show Line Numbers

cat -n file.txt

πŸ”Ή This displays the file with line numbers.

8️⃣ Remove Extra Blank Lines

cat -s file.txt

πŸ”Ή This suppresses consecutive empty lines.

9️⃣ Display File with less (For Large Files)

cat largefile.txt | less

πŸ”Ή This allows scrolling through the file using less.

πŸ”Ÿ Display File in Reverse Order

tac file.txt

πŸ”Ή tac is like cat, but displays lines in reverse order.


🎯 Practical Examples

πŸ”Ή 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

πŸ“ Summary

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