Bash cat Command – Complete Guide to Concatenating and Displaying Files

Introduction to cat

The cat command (short for "concatenate") is one of the most fundamental and versatile commands in Unix/Linux systems. It's primarily used to display file contents, combine files, and create files. Understanding cat is essential for efficient command-line file manipulation.

Basic Syntax

cat [options] [file...]

1. Basic Usage

Display File Contents

# Display a single file
cat file.txt
# Display multiple files
cat file1.txt file2.txt
# Display with line numbers (non-blank lines)
cat -b file.txt
# Display with line numbers (all lines)
cat -n file.txt

Output Example

$ cat file.txt
This is line 1
This is line 2
This is line 3
$ cat -n file.txt
1  This is line 1
2  This is line 2
3  This is line 3

2. Creating and Appending Files

Create New Files

# Create a new file (overwrites if exists)
cat > newfile.txt
Type your content here
Press Ctrl+D to save and exit
# Create with multiple lines
cat > script.sh << 'EOF'
#!/bin/bash
echo "Hello World"
EOF

Append to Files

# Append content to existing file
cat >> existing.txt
New content to append
Press Ctrl+D to save and exit
# Append another file
cat file2.txt >> file1.txt

Combine Files

# Combine multiple files into one
cat file1.txt file2.txt file3.txt > combined.txt
# Append multiple files
cat file4.txt file5.txt >> combined.txt
# Combine all text files in directory
cat *.txt > all_text.txt

3. Common Options

Line Numbering

# -n: Number all lines
cat -n file.txt
# -b: Number non-empty lines only
cat -b file.txt
# -s: Squeeze multiple blank lines into one
cat -s file.txt

Special Character Display

# -v: Display non-printing characters
cat -v file.txt
# -t: Display tabs as ^I
cat -t file.txt
# -e: Display $ at end of lines (with -v)
cat -e file.txt
# -A: Equivalent to -vET (show all)
cat -A file.txt

Examples with Special Characters

$ cat -A file.txt
This is line 1$
This line has a tab^Ihere$
$
This line has spaces    here$

4. Working with Multiple Files

Concatenation Operations

# Display multiple files sequentially
cat file1.txt file2.txt file3.txt
# Add headers between files
for file in *.txt; do
echo "=== $file ==="
cat "$file"
echo
done
# Interleave files line by line
paste file1.txt file2.txt
# Number lines across multiple files
cat -n file1.txt file2.txt

File Operations

# Merge files with separator
cat file1.txt <(echo "---") file2.txt > merged.txt
# Combine and sort
cat file1.txt file2.txt | sort > sorted.txt
# Remove duplicates after concatenation
cat file1.txt file2.txt | sort -u > unique.txt

5. Advanced Usage Patterns

Here Documents

# Basic here document
cat << EOF
This is a multi-line
text block that will be
displayed as is.
EOF
# With variable expansion
name="John"
cat << EOF
Hello $name,
Welcome to the system.
EOF
# Disable variable expansion with quotes
cat << 'EOF'
Variable $name will not be expanded
EOF
# Indented here document (with tabs)
cat <<- EOF
This text is indented with tabs
but will be displayed without
the leading tabs.
EOF

Process Substitution

# Compare two command outputs
cat <(ls -l) <(ls -l /etc)
# Combine with other commands
cat <(grep "ERROR" log1.txt) <(grep "ERROR" log2.txt) > errors.txt
# Create temporary file-like structures
cat <(echo "Header") data.txt <(echo "Footer") > full.txt

Piping and Redirection

# Pipe to other commands
cat file.txt | grep "pattern" | sort | uniq
# Redirect to multiple files (using tee)
cat file.txt | tee copy1.txt copy2.txt
# Capture output
output=$(cat file.txt)
# Append to multiple files
cat file.txt | tee -a log1.txt log2.txt

6. Practical Examples

File Viewing and Navigation

# View with line numbers and squeeze blanks
cat -ns file.txt
# View with non-printing characters shown
cat -v binary.dat
# View multiple files with headers
for f in *.log; do echo "=== $f ==="; cat "$f"; done
# View specific line ranges (using sed)
cat file.txt | sed -n '10,20p'  # Lines 10-20

File Processing

# Remove duplicate lines
cat file.txt | sort | uniq > file_unique.txt
# Add line numbers to non-empty lines
cat -b file.txt > numbered.txt
# Convert tabs to spaces
cat -t file.txt | sed 's/\^I/    /g' > expanded.txt
# Remove comments and blank lines
cat config.txt | grep -v '^#' | grep -v '^$' > clean.conf

Log File Analysis

# Combine and analyze multiple log files
cat /var/log/nginx/access.log* | grep "404" | cut -d' ' -f1 | sort | uniq -c
# Monitor logs in real-time (with tail)
tail -f access.log | cat
# Extract errors from multiple logs
cat app.log web.log db.log | grep -i "error" > errors.txt

Configuration Management

# Generate configuration from templates
cat header.conf > final.conf
cat >> final.conf << 'EOF'
# Custom settings
max_connections = 100
timeout = 30
EOF
cat footer.conf >> final.conf
# Merge configuration files
cat /etc/app/*.conf > app.conf

7. Working with Binary Files

Display Binary Files

# Show non-printing characters
cat -v binary.dat
# Pipe to hexdump for better viewing
cat binary.dat | hexdump -C
# Compare binary files
cat file1.bin file2.bin | wc -c

Binary Operations

# Split binary file (using split is better)
cat large.bin | split -b 1M - part_
# Combine binary parts
cat part_* > reconstructed.bin
# Check if files are identical
cmp file1.bin file2.bin || echo "Files differ"

8. Error Handling and Special Cases

Handling Large Files

# View large files with pagination
cat hugefile.txt | less
# Pipe to head for preview
cat hugefile.txt | head -50
# Check file size before viewing
ls -lh hugefile.txt
cat hugefile.txt | wc -l

Special Files and Devices

# Display device information
cat /proc/cpuinfo
cat /proc/meminfo
cat /proc/version
# Read from special files
cat /dev/random | head -c 16 | hexdump -C
# Write to devices (dangerous!)
# echo "data" | cat > /dev/sda  # DON'T DO THIS!

Error Cases

# File doesn't exist
cat nonexistent.txt
# Output: cat: nonexistent.txt: No such file or directory
# Permission denied
cat /etc/shadow
# Output: cat: /etc/shadow: Permission denied
# Directory instead of file
cat /etc
# Output: cat: /etc: Is a directory

9. cat in Shell Scripts

Script Examples

File Processor Script:

#!/bin/bash
# process.sh - Process multiple files
process_files() {
local output="processed_$(date +%Y%m%d).txt"
for file in "$@"; do
if [[ -f "$file" ]]; then
echo "Processing: $file"
cat "$file" | sed 's/old/new/g' >> "$output"
else
echo "Warning: $file not found" >&2
fi
done
echo "Output written to $output"
}
process_files "$@"

Config Generator:

#!/bin/bash
# generate_config.sh
generate_nginx_config() {
local domain="$1"
local port="${2:-80}"
cat << EOF > "/etc/nginx/sites-available/$domain"
server {
listen $port;
server_name $domain www.$domain;
root /var/www/$domain;
index index.html;
location / {
try_files \$uri \$uri/ =404;
}
access_log /var/log/nginx/$domain.access.log;
error_log /var/log/nginx/$domain.error.log;
}
EOF
echo "Configuration generated for $domain"
}
generate_nginx_config "example.com" 8080

Log Aggregator:

#!/bin/bash
# aggregate_logs.sh
aggregate_logs() {
local date=$(date +%Y-%m-%d)
local report="report_$date.txt"
{
echo "=== Log Report for $date ==="
echo
echo "=== System Logs ==="
cat /var/log/syslog | grep "$date" | tail -20
echo
echo "=== Application Logs ==="
cat /var/log/app.log | grep "$date" | tail -20
echo
echo "=== Error Summary ==="
cat /var/log/syslog /var/log/app.log | 
grep -i "error" | 
grep "$date" | 
sort | uniq -c | sort -rn
} > "$report"
echo "Report generated: $report"
}
aggregate_logs

10. Performance Considerations

Large File Handling

# For very large files, use alternatives
cat hugefile.txt > /dev/null  # Discard output (tests read speed)
# Compare with alternatives
time cat hugefile.txt > /dev/null
time dd if=hugefile.txt of=/dev/null bs=1M
time head -c 100M hugefile.txt > /dev/null

Optimization Tips

# Avoid unnecessary cat (useless use of cat)
# Instead of:
cat file.txt | grep pattern
# Use:
grep pattern file.txt
# Instead of:
cat file.txt | sort | uniq
# Use:
sort -u file.txt
# When you need multiple operations, cat is appropriate:
cat file.txt | grep pattern | sort | uniq > output.txt

Memory Usage

# cat reads in chunks, memory efficient for large files
# Monitor memory usage
/usr/bin/time -v cat hugefile.txt > /dev/null
# For very large concatenations
find . -name "*.log" -exec cat {} \; > all.log
# vs
cat *.log > all.log  # May hit argument limit

11. Alternatives to cat

Command Comparison

CommandPurposeExample
catDisplay/concatenate filescat file.txt
tacDisplay files in reversetac file.txt
headDisplay first lineshead -n 10 file.txt
tailDisplay last linestail -n 10 file.txt
morePage through filesmore file.txt
lessAdvanced pagerless file.txt
nlNumber linesnl file.txt
odOctal dumpod -c file.txt
hexdumpHexadecimal viewhexdump -C file.txt

When to Use Alternatives

# View beginning of file
head file.txt
# View end of file
tail file.txt
# View in reverse order
tac file.txt
# Page through large file
less file.txt
# View with syntax highlighting
bat file.txt  # External tool

12. Common Patterns and Recipes

File Manipulation Recipes

# Add header to file
{ echo "Header"; cat file.txt; } > file_with_header.txt
# Add footer
{ cat file.txt; echo "Footer"; } > file_with_footer.txt
# Wrap content
{
echo "BEGIN"
cat file.txt
echo "END"
} > wrapped.txt
# Number lines and add to new file
cat -n file.txt > numbered.txt

Data Processing Recipes

# Extract columns (using cut)
cat data.txt | cut -d',' -f1,3 > columns.txt
# Join files
cat file1.txt <(echo) file2.txt > combined.txt
# Create CSV
{
echo "Name,Age,City"
cat data.txt | while read line; do
echo "$line"
done
} > data.csv

System Administration Recipes

# Check multiple log files
cat /var/log/{syslog,auth.log,app.log} | grep ERROR
# System information report
{
echo "=== System Report $(date) ==="
echo "=== Hostname ==="
cat /etc/hostname
echo "=== OS Version ==="
cat /etc/os-release
echo "=== Memory Info ==="
cat /proc/meminfo | head -5
} > system_report.txt
# User list
cat /etc/passwd | cut -d: -f1 | sort

13. Interactive Use Tips

Keyboard Shortcuts

# While using cat interactively:
# Ctrl+D - End of file (save and exit)
# Ctrl+C - Cancel/interrupt
# Ctrl+Z - Suspend (then 'fg' to resume)
# Create multi-line input
cat > file.txt << 'EOF'
Line 1
Line 2
EOF

Useful Aliases

# Add to .bashrc
alias c='cat'
alias cn='cat -n'
alias cb='cat -b'
alias cs='cat -s'
alias ca='cat -A'
alias cl='cat | less'
alias cg='cat | grep'
# Display with syntax highlighting (requires bat)
alias bat='bat --paging=never'

14. Security Considerations

Safe Practices

# Avoid viewing sensitive files
cat /etc/shadow  # Requires root
sudo cat /etc/shadow  # Use with caution
# Be careful with output redirection
cat > important_config.conf  # Will overwrite!
cat >> important_config.conf  # Appends safely
# Avoid command injection
# Dangerous:
cat $(echo file.txt)  # Command substitution can be exploited
# Safer:
cat "file.txt"

File Permissions

# Check permissions before reading
ls -l file.txt
cat file.txt  # Won't work without read permission
# Use sudo when necessary
sudo cat /var/log/secure

15. Troubleshooting

Common Problems and Solutions

Problem: "cat: command not found"

# Solution: Install coreutils
apt-get install coreutils  # Debian/Ubuntu
yum install coreutils      # RHEL/CentOS

Problem: "Argument list too long"

# When using cat *.txt with many files
# Instead use:
find . -name "*.txt" -exec cat {} \; > all.txt
# Or use xargs
find . -name "*.txt" -print0 | xargs -0 cat > all.txt

Problem: Binary file garbled display

# Use hexdump for binary files
cat binary.dat | hexdump -C | less
# Or od command
od -c binary.dat | less

Problem: Mixed line endings

# Convert Windows line endings
cat file.txt | tr -d '\r' > unix.txt
# View line endings
cat -e file.txt

16. Integration with Other Commands

Pipeline Examples

# Statistics pipeline
cat access.log | awk '{print $1}' | sort | uniq -c | sort -rn | head -10
# Search and replace
cat file.txt | sed 's/old/new/g' > newfile.txt
# Format and display
cat file.json | python -m json.tool  # Pretty print JSON
cat file.xml | xmllint --format -    # Pretty print XML

With Version Control

# View file from git
git show HEAD:file.txt | cat
# Compare versions
git diff HEAD~1 HEAD file.txt | cat
# Create patch
git diff | cat > changes.patch

Conclusion

The cat command is a fundamental tool in Unix/Linux systems with versatile applications:

Key Points Summary

  1. Basic Operations: Display, create, and concatenate files
  2. Options: Line numbering (-n, -b), show special chars (-A, -v, -t, -e)
  3. Redirection: Create (>), append (>>), combine files
  4. Here Documents: Multi-line input with cat << EOF
  5. Process Substitution: Work with command outputs as files
  6. Scripting: Essential for file processing in shell scripts
  7. Performance: Generally efficient, but avoid "useless use of cat"

Best Practices

  1. Use appropriate options for your use case
  2. Avoid useless use of cat (let commands read files directly)
  3. Be careful with redirection (don't accidentally overwrite)
  4. Use pagers (less, more) for large files
  5. Check permissions before trying to read files
  6. Consider alternatives when appropriate (head, tail, tac)

Quick Reference

OperationCommand
Display filecat file.txt
Create filecat > new.txt
Append filecat >> existing.txt
Combine filescat f1 f2 > combined
Number linescat -n file.txt
Show special charscat -A file.txt
Squeeze blankscat -s file.txt
Here documentcat << EOF > file.txt

The cat command's simplicity and power make it an indispensable tool in any command-line user's toolkit. Mastering cat opens up countless possibilities for file manipulation and data processing.

Leave a Reply

Your email address will not be published. Required fields are marked *


Macro Nepal Helper