The cat
(concatenate) command is one of the most frequently used commands in Linux. It reads data from files and outputs their contents. It can also be used to create and combine files.
Basic Syntax
cat [options] [file(s)]
Common Options
-n
: Number all output lines-b
: Number non-blank output lines-s
: Suppress repeated empty output lines-A
: Show all non-printing characters-v
: Show non-printing characters except tabs and line feeds-E
: Display $ at end of each line-T
: Display TAB characters as ^I
Real-World Examples
1. View File Contents
# Display single file
$ cat file.txt
# Display multiple files
$ cat file1.txt file2.txt
# Display with line numbers
$ cat -n file.txt
2. File Creation and Input
# Create new file from keyboard input
$ cat > newfile.txt
Type your text here
Press Ctrl+D when done
# Append to existing file
$ cat >> existing.txt
Add more text
Press Ctrl+D when done
3. File Combination
# Combine files
$ cat file1.txt file2.txt > combined.txt
# Append file to another
$ cat appendfile.txt >> mainfile.txt
Common Use Cases
-
Quick File Viewing
# View configuration file cat /etc/nginx/nginx.conf # View system information cat /proc/cpuinfo
-
File Creation
# Create script file cat > script.sh << 'EOF' #!/bin/bash echo "Hello, World!" EOF
-
File Concatenation
# Combine log files cat access.log.* > combined_logs.txt # Merge configuration files cat config.d/* > full_config.conf
Tips and Tricks
-
Display Special Characters
# Show all special characters cat -A file.txt # Show line endings cat -E file.txt
-
Handle Large Files
# Use with head/tail cat large_file.txt | head -n 20 # Use with less for pagination cat large_file.txt | less
-
Multiple File Operations
# Sort and combine files cat file*.txt | sort > sorted_combined.txt # Filter and save cat logs.txt | grep "error" > errors.txt
Best Practices
-
Use Appropriate Tools
# For large files, prefer less less large_file.txt # For viewing end of file tail -f log_file.txt
-
Handle Binary Files
# Check if file is binary file myfile # Use hexdump for binary files hexdump -C binary_file
-
Error Handling
# Check if file exists cat file.txt 2>/dev/null || echo "File not found"
Common Errors and Solutions
-
No Such File
# Check file existence first [ -f file.txt ] && cat file.txt
-
Permission Denied
# Use sudo if needed sudo cat /etc/shadow
-
Binary File Warning
# Use appropriate viewer strings binary_file
Related Commands
less
: View files with paginationmore
: Simple file viewerhead
: View beginning of filetail
: View end of filetac
: View file in reverse
Advanced Usage
1. Here Documents
cat << EOF > config.ini
[Settings]
User = ${USER}
Home = ${HOME}
Date = $(date)
EOF
2. Process Substitution
# Compare command outputs
cat <(ls -l) <(ls -la)
# Process multiple inputs
cat <(sort file1.txt) <(sort file2.txt) > sorted_all.txt
3. Text Processing
# Remove blank lines
cat -s input.txt > output.txt
# Number non-blank lines
cat -b code.txt > numbered_code.txt
Scripting Examples
1. File Processing
#!/bin/bash
# Process multiple files
for file in *.txt; do
echo "=== $file ==="
cat -n "$file"
echo
done
2. Log Analysis
#!/bin/bash
# Extract and format log entries
cat access.log | while read line; do
if [[ $line == *"ERROR"* ]]; then
echo "[!] $line"
fi
done
3. File Generation
#!/bin/bash
# Generate configuration file
cat > config.json << EOF
{
"user": "$(whoami)",
"date": "$(date)",
"system": "$(uname -a)"
}
EOF
Remember that while cat
is simple, it’s incredibly versatile. It’s not just for viewing files – it’s a fundamental tool in text processing pipelines and shell scripting. However, for large files or specific viewing needs, consider using specialized tools like less
, tail
, or grep
.
FAQ
What does the cat
command do?
The cat
command is used to read and concatenate files. It can display file contents, create new files, and combine multiple files into one.
How can I display line numbers using cat
?
To display line numbers, use the -n
option with the cat
command:
cat -n file.txt
This will number all output lines.
Can cat
be used to create files?
Yes, you can create files using cat
by redirecting input to a new file:
cat > newfile.txt
Type your content and press Ctrl+D
to save.
How do I combine multiple files using cat
?
To combine files, list them in order and redirect the output to a new file:
cat file1.txt file2.txt > combined.txt
This will merge the contents of file1.txt
and file2.txt
into combined.txt
.
What should I do if cat
displays a “Permission Denied” error?
If you encounter a “Permission Denied” error, you may need to use sudo
to run the command with elevated privileges:
sudo cat /etc/shadow
Be cautious when using sudo
to avoid unintended changes to system files.