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
โšก