Skip to main content

Command documentation sourced from the linux-command project This comprehensive command reference is part of the linux-command documentation project.

cat - Concatenate and Display File Contents

The cat (concatenate) command is one of the most fundamental and frequently used utilities in Linux/Unix systems. It reads files sequentially, writing their contents to standard output. Despite its simplicity, cat is incredibly versatile for file operations, text processing, pipeline operations, and system administration tasks. The command's name reflects its primary purpose of concatenating files, but it's commonly used for viewing file contents, creating files, and combining multiple files. Its ability to work with standard input/output makes it an essential tool in shell scripting and command-line workflows.

Basic Syntax

cat [OPTIONS] [FILE]...

When no FILE is specified, or when FILE is '-', cat reads from standard input.

Common Options

Display Control Options

  • -n, --number - Number all output lines starting from 1
  • -b, --number-nonblank - Number only non-empty output lines
  • -s, --squeeze-blank - Suppress repeated empty output lines to a single line
  • -v, --show-nonprinting - Use ^ and M- notation for non-printing characters

Character Display Options

  • -E, --show-ends - Display $ at the end of each line
  • -T, --show-tabs - Display TAB characters as ^I
  • -A, --show-all - Equivalent to -vET (shows all non-printing characters, line endings, and tabs)
  • -e - Equivalent to -vE
  • -t - Equivalent to -vT

Help and Version

  • --help - Display help information and exit
  • --version - Output version information and exit

Usage Examples

Basic File Operations

Displaying File Contents

# Display single file contents
cat filename.txt

# Display multiple files in sequence
cat file1.txt file2.txt file3.txt

# Display all files matching a pattern
cat *.txt

# Display file contents with line numbers
cat -n script.sh

# Display file with numbered non-blank lines only
cat -b document.txt

# Read from standard input (interactive)
cat
# Type your content here, press Ctrl+D to end

File Creation and Editing

# Create new file (overwrites if exists)
cat > newfile.txt
Enter your content here
Press Ctrl+D to save

# Append content to existing file
cat >> existing.txt
Additional content here
Press Ctrl+D to save

# Create file using here document
cat > config.txt << EOF
server_name = example.com
port = 8080
debug = true
EOF

# Create script with here document
cat > script.sh << 'EOF'
#!/bin/bash
echo "Hello World"
date
EOF
chmod +x script.sh

File Concatenation and Combining

# Concatenate multiple files into one
cat header.txt content.txt footer.txt > complete_document.txt

# Combine all text files in directory
cat chapter*.txt > complete_book.txt

# Add headers to multiple files
for file in *.txt; do
cat header.txt "$file" > "temp_$file"
mv "temp_$file" "$file"
done

# Create backup with timestamp
cat config.txt > "backup_config_$(date +%Y%m%d_%H%M%S).txt"

# Append files to existing document
cat appendix.txt >> main_document.txt

Advanced Display Options

Line Numbering and Formatting

# Number all lines with tab-separated format
cat -n file.txt

# Number only non-blank lines
cat -b file.txt

# Squeeze multiple blank lines into one
cat -s file.txt

# Combine multiple options
cat -ns file.txt # Number non-blank lines, squeeze blanks

# Show line endings and tabs
cat -ET file.txt

# Show all non-printing characters
cat -A file.txt

# Display with custom formatting
cat -n file.txt | grep "pattern"

Special Character Display

# Show invisible characters for debugging
cat -A script.sh

# Check file format (Windows vs Unix line endings)
cat -E file.txt

# Find tabs in configuration files
cat -T config.ini

# Display control characters
cat -v binary_file.txt

Text Processing and Filtering

Content Analysis

# Remove blank lines
cat -s file.txt > clean_file.txt

# Add line numbers to source code
cat -n source.py > numbered_source.py

# Create files with line numbers for documentation
cat -n *.py > code_listing.txt

# Show file statistics
cat file.txt | wc -l # Count lines
cat file.txt | wc -w # Count words
cat file.txt | wc -c # Count characters

Data Transformation

# Convert to uppercase
cat file.txt | tr '[:lower:]' '[:upper:]'

# Replace text patterns
cat file.txt | sed 's/old/new/g'

# Sort and remove duplicates
cat file.txt | sort | uniq

# Extract specific columns
cat file.txt | awk '{print $1, $3}'

# Filter lines containing specific patterns
cat file.txt | grep "ERROR"

# Remove comments and blank lines from config files
cat config.ini | grep -v '^#' | grep -v '^$'

Practical Examples

System Administration

System Information and Configuration

# View system configuration files
cat /etc/hosts
cat /etc/fstab
cat /etc/passwd
cat /etc/group

# Check kernel and system information
cat /proc/version
cat /proc/cpuinfo
cat /proc/meminfo
cat /proc/mounts

# View network configuration
cat /etc/resolv.conf
cat /etc/network/interfaces
cat /etc/hosts.allow

# Check user information
cat /etc/passwd | grep username
cat /etc/group | grep groupname

# Monitor system logs
cat /var/log/syslog | tail -20
cat /var/log/auth.log | grep "Failed password"

File System Operations

# Create system backup scripts
cat > backup.sh << 'EOF'
#!/bin/bash
DATE=$(date +%Y%m%d)
tar -czf "backup_$DATE.tar.gz" /home/user
echo "Backup completed: backup_$DATE.tar.gz"
EOF

# View disk usage information
cat /proc/partitions
cat /proc/filesystems

# Check hardware information
cat /proc/dma
cat /proc/interrupts
cat /proc/devices

Development Workflow

Source Code Management

# Combine source files for compilation
cat *.c > combined_source.c

# Create header guards automatically
cat > protected_header.h << 'EOF'
#ifndef PROTECTED_HEADER_H
#define PROTECTED_HEADER_H

// Header content here

#endif // PROTECTED_HEADER_H
EOF

# Generate build configuration
cat > Makefile << 'EOF'
CC=gcc
CFLAGS=-Wall -g
TARGET=program

all: $(TARGET)

$(TARGET): main.c
$(CC) $(CFLAGS) -o $(TARGET) main.c

clean:
rm -f $(TARGET)
EOF

# Create documentation skeleton
cat > README.md << 'EOF'
# Project Title

## Description
Project description here.

## Installation
Installation instructions here.

## Usage
Usage instructions here.

## License
License information here.
EOF

Testing and Debugging

# Create test input files
cat > test_input.txt << EOF
Line 1
Line 2
Line 3
EOF

# Generate test data
for i in {1..100}; do
echo "Test line $i: Random data $RANDOM"
done | cat > test_data.txt

# Create configuration files for testing
cat > test_config.ini << EOF
[database]
host=localhost
port=5432
username=test
password=test123

[logging]
level=debug
file=test.log
EOF

Data Processing

Log File Analysis

# Extract error messages from logs
cat /var/log/application.log | grep "ERROR"

# Count different types of log entries
cat access.log | awk '{print $1}' | sort | uniq -c | sort -nr

# Generate log summary
cat access.log | awk '
BEGIN { count=0 }
/200/ { success++ }
/404/ { notfound++ }
/500/ { error++ }
END {
print "Success:", success
print "Not Found:", notfound
print "Server Error:", error
print "Total:", NR
}'

# Filter logs by time range
cat access.log | grep "2023-11-28"

# Extract IP addresses from logs
cat access.log | awk '{print $1}' | sort | uniq

CSV and Data File Processing

# Combine CSV files
cat data1.csv data2.csv data3.csv > all_data.csv

# Remove headers from all but first file
cat headers.csv $(ls data_*.csv | tail -n +2 | sed 's/^/<(tail -n +2 /; s/$/)/') > combined.csv

# Add line numbers to data for analysis
cat -n data.csv > numbered_data.csv

# Create data validation script
cat > validate_data.py << 'EOF'
#!/usr/bin/env python3
import sys
import csv

with open(sys.argv[1], 'r') as f:
reader = csv.reader(f)
for row_num, row in enumerate(reader, 1):
if len(row) != expected_columns:
print(f"Row {row_num}: Invalid column count")
EOF

# Generate data statistics
cat data.csv | awk -F, 'NR>1 {sum+=$3; count++} END {print "Average:", sum/count}'

Shell Scripting and Automation

Script Generation

# Create batch processing script
cat > process_files.sh << 'EOF'
#!/bin/bash
for file in *.txt; do
echo "Processing $file..."
# Add processing commands here
cat -n "$file" > "numbered_$file"
done
echo "Processing complete!"
EOF

# Generate deployment script
cat > deploy.sh << 'EOF'
#!/bin/bash
echo "Starting deployment..."
# Backup current version
cat current_version.txt > "backup_$(date +%Y%m%d_%H%M%S).txt"
# Deploy new version
echo "Deployment complete!"
EOF

# Create system monitoring script
cat > monitor.sh << 'EOF'
#!/bin/bash
while true; do
echo "=== $(date) ==="
cat /proc/loadavg
cat /proc/meminfo | grep -E "(MemTotal|MemFree)"
sleep 60
done
EOF

Configuration Management

# Create environment configuration
cat > .env << EOF
DATABASE_URL=localhost:5432
API_KEY=your_api_key_here
DEBUG=false
LOG_LEVEL=info
EOF

# Generate Apache configuration
cat > site.conf << 'EOF'
<VirtualHost *:80>
ServerName example.com
DocumentRoot /var/www/example

<Directory /var/www/example>
AllowOverride All
Require all granted
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
EOF

# Create nginx configuration
cat > nginx.conf << 'EOF'
server {
listen 80;
server_name example.com;
root /var/www/example;
index index.html;

location / {
try_files $uri $uri/ =404;
}

location /api/ {
proxy_pass http://localhost:3000;
}
}
EOF

Advanced Usage

Pipeline Operations

Complex Data Processing Pipelines

# Multi-stage text processing
cat data.txt | grep "pattern" | sort | uniq -c | sort -nr

# Data extraction and formatting
cat log.txt | awk '{print $1, $5, $7}' | sed 's/[][]//g' | sort

# Real-time log monitoring with processing
tail -f /var/log/access.log | cat | grep "404"

# File content transformation
cat file.txt | tr '[:lower:]' '[:upper:]' | sed 's/OLD/NEW/g' > processed.txt

# Extract and process specific fields
cat data.csv | cut -d',' -f1,3,5 | sort | uniq > processed_data.csv

Redirection and Input/Output Management

# Multiple file processing
cat file1.txt file2.txt | grep "error" > errors.log

# Standard input processing
echo "test input" | cat -n

# Here document with variable substitution
cat > report.txt << EOF
Report generated: $(date)
System: $(uname -a)
User: $(whoami)
EOF

# Process substitution
cat <(grep "error" log1.txt) <(grep "error" log2.txt) > all_errors.txt

# Create files from command output
ls -la | cat > file_list.txt
ps aux | cat > process_list.txt

File Analysis and Debugging

Binary File Analysis

# Check file type and content
cat -v binary_file | head -10

# Show file encoding and special characters
cat -A suspicious_file.txt

# Compare files using cat and diff
cat file1.txt > temp1.txt
cat file2.txt > temp2.txt
diff temp1.txt temp2.txt

# Extract text from mixed content
cat mixed_file | strings > text_only.txt

Performance Analysis

# Measure processing time
time cat large_file.txt | wc -l

# Compare different methods
time cat file.txt | grep pattern
time grep pattern file.txt

# Memory usage analysis
/usr/bin/time -v cat huge_file.txt > /dev/null

Integration with System Tools

Working with Archives and Compression

# Create compressed archives with content
cat file1.txt file2.txt | gzip > archive.gz

# Extract and view compressed content
gunzip -c archive.gz | cat

# Create tar archives with specific files
cat file_list.txt | xargs tar -cvf archive.tar

# Process compressed logs
zcat access.log.gz | cat | grep "404"

Network Operations

# Create network configuration files
cat > /etc/network/interfaces << EOF
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
EOF

# Generate hosts file entries
cat >> /etc/hosts << EOF
192.168.1.100 server1.example.com
192.168.1.101 server2.example.com
EOF

Best Practices

File Handling

  1. Check file existence before reading with [ -f "file.txt" ] && cat file.txt
  2. Use less for large files instead of cat to avoid memory issues
  3. Be careful with redirection to avoid accidentally overwriting files
  4. Use cat -v for suspicious files to detect hidden characters
  5. Combine with other tools for powerful text processing pipelines

Performance Considerations

  1. Use appropriate tools: head, tail, grep for specific operations
  2. Avoid cat file | grep pattern - use grep pattern file instead
  3. Use cat -n only when line numbers are needed
  4. Consider memory usage when working with very large files
  5. Use streaming operations for processing large datasets

Security and Safety

  1. Never redirect to system files without proper backup
  2. Use sudo carefully when reading protected files
  3. Check file permissions before attempting to read
  4. Validate input when creating files from user input
  5. Use appropriate quoting for filenames with special characters

Shell Scripting

  1. Use quotes for filenames with spaces or special characters
  2. Error handling: check if files exist before processing
  3. Use appropriate redirection for error output
  4. Document complex scripts with comments
  5. Test with sample data before processing important files

Performance Tips

Memory Efficiency

  1. Stream processing: Use pipes instead of temporary files when possible
  2. Avoid unnecessary buffering: Process data as it's read
  3. Use specific tools: head, tail, grep are more memory-efficient
  4. Process large files in chunks: Split large operations into smaller parts
  5. Clean up temporary files: Remove intermediate files when done

Speed Optimization

  1. Combine commands: Use single command chains instead of multiple operations
  2. Avoid redundant operations: Don't use cat when tools can read files directly
  3. Use appropriate tools: Choose the right tool for each specific task
  4. Minimize disk I/O: Reduce the number of times data is written to disk
  5. Parallel processing: Use background processes for independent operations

Troubleshooting

Common Issues

File Access Problems

# File not found
cat nonexistent.txt
# Solution: Check file path and existence
ls -la file.txt
find . -name "file.txt"

# Permission denied
cat /etc/shadow
# Solution: Check permissions and use appropriate privileges
ls -l /etc/shadow
sudo cat /etc/shadow

# Binary file display issues
cat binary_file
# Solution: Use file command first and appropriate viewers
file binary_file
hexdump -C binary_file

Encoding and Character Issues

# Display issues with special characters
cat file_with_unicode.txt
# Solution: Check and handle encoding
file -bi file_with_unicode.txt
iconv -f utf-8 -t latin1 file_with_unicode.txt

# Line ending issues (Windows vs Unix)
cat -E file.txt
# Solution: Convert line endings
dos2unix file.txt
sed 's/\r$//' file.txt > unix_file.txt

Performance Issues

# Slow processing of large files
cat huge_file.txt | grep pattern
# Solution: Use grep directly
grep pattern huge_file.txt

# Memory issues with very large files
cat enormous_file.txt
# Solution: Use less or stream processing
less enormous_file.txt
head -n 100 enormous_file.txt

Redirection Problems

# Accidental file overwriting
cat important_file.txt > backup.txt
# Solution: Use append or create backups first
cat important_file.txt > backup_$(date +%Y%m%d).txt

# Permission issues with output redirection
cat file.txt > /protected/directory/file.txt
# Solution: Check directory permissions
ls -ld /protected/directory/
sudo bash -c "cat file.txt > /protected/directory/file.txt"
  • less - View files page by page with search capabilities
  • more - Simple file pager for basic viewing
  • head - Display first lines of a file
  • tail - Display last lines of a file
  • tac - Concatenate and print files in reverse
  • nl - Number lines of files with more options
  • od - Octal dump for binary file analysis
  • hexdump - Display file contents in hexadecimal
  • tee - Read from stdin and write to stdout and files
  • paste - Merge lines of files
  • split - Split files into smaller pieces
  • join - Join lines of two files on a common field

The cat command is a versatile and essential tool in the Linux/Unix toolkit. While seemingly simple, its ability to concatenate files, work with standard input/output, and integrate seamlessly with other commands through pipelines makes it indispensable for file operations, text processing, and system administration tasks. Mastering cat and its various options significantly enhances command-line productivity and enables powerful file manipulation workflows.