The cp
(copy) command is used to copy files and directories in Linux. This guide covers everything from basic copying to advanced operations with attribute preservation.
Basic Syntax
cp [options] source destination
Common Options
-r, -R
: Recursively copy directories-p
: Preserve mode, ownership, timestamps-a
: Archive mode (same as -dR —preserve=all)-i
: Interactive mode (prompt before overwrite)-u
: Update mode (copy only when source is newer)-v
: Verbose mode (explain what’s being done)-l
: Create hard links instead of copying-s
: Create symbolic links instead of copying
Real-World Examples
1. Basic File Copying
# Copy single file
$ cp source.txt destination.txt
# Copy multiple files
$ cp file1.txt file2.txt directory/
# Copy with specific name
$ cp original.txt backup_original.txt
2. Directory Copying
# Copy directory recursively
$ cp -r source_dir/ destination_dir/
# Copy directory contents
$ cp -r source_dir/* destination_dir/
# Copy with full preservation
$ cp -a /source/dir/ /dest/dir/
3. Advanced Copying
# Copy only newer files
$ cp -u *.txt backup/
# Copy with progress
$ cp -v large_file.iso /media/backup/
# Create backup with timestamp
$ cp file.txt file.txt.$(date +%Y%m%d)
Common Use Cases
-
Backup Operations
# Backup configuration cp -a /etc/nginx/nginx.conf{,.backup} # Backup with date cp -a config.ini config.ini.$(date +%Y%m%d)
-
Project Management
# Copy project template cp -r template/ new_project/ # Copy specific file types cp -r src/*.{js,css} dist/
-
System Administration
# Copy with permissions sudo cp -p /etc/passwd{,.bak} # Copy system files sudo cp -a /var/lib/mysql/* /backup/mysql/
Tips and Tricks
-
Preserve Attributes
# Copy with all attributes cp -av source_dir/ dest_dir/ # Copy with specific attributes cp --preserve=mode,ownership file1 file2
-
Safe Copying
# Interactive mode cp -i important.txt backup/ # No clobber (don't overwrite) cp -n source.txt destination.txt
-
Efficient Copying
# Update only cp -ru source_dir/ backup_dir/ # Copy with hard links cp -al source_dir/ snapshot_dir/
Best Practices
-
Use Appropriate Options
# For configuration files sudo cp -a /etc/config.conf{,.bak} # For user data cp -rp ~/documents/* ~/backup/
-
Verify Copies
# Use verbose mode cp -v file.txt backup/ # Check with diff diff -r source_dir/ dest_dir/
-
Handle Special Files
# Copy device files cp -a /dev/sda1 /dev/backup_sda1 # Copy sparse files cp --sparse=always large_file backup/
Common Errors and Solutions
-
Permission Denied
# Solution: Use sudo sudo cp restricted.txt /etc/
-
No Space Left
# Check space first df -h # Use compression tar czf - source_dir | (cd /dest && tar xzf -)
-
File Exists
# Solution: Use -i or -n cp -i source.txt destination.txt
Related Commands
mv
: Move/rename filesrsync
: Advanced file copyingdd
: Copy and convert filestar
: Archive files
Advanced Usage
1. Copying with Pattern Matching
# Copy multiple file types
cp -r src/*.{html,css,js} dist/
# Exclude patterns
cp -r source_dir/ dest_dir/ --exclude="*.tmp"
2. Backup Strategies
#!/bin/bash
# Incremental backup script
backup_dir="/backup/$(date +%Y-%m-%d)"
cp -al /backup/latest "$backup_dir"
cp -au /source/* "$backup_dir/"
ln -nsf "$backup_dir" /backup/latest
3. Network Copying
# Using cp with ssh
tar cf - source_dir | ssh user@remote "cd /dest && tar xf -"
# Using rsync (alternative)
rsync -av source_dir/ user@remote:/dest/dir/
Special Use Cases
1. Large File Copying
# Copy with progress
pv source.iso | cp /dev/stdin destination.iso
# Resume interrupted copy
rsync --partial --progress source.iso dest/
2. Directory Structure Only
# Copy directory structure without files
cp -R --attributes-only source_dir/ dest_dir/
3. Sparse File Handling
# Efficiently copy sparse files
cp --sparse=always large_sparse_file backup/
Remember that cp
is a fundamental command for file management in Linux. Understanding its options and best practices helps ensure safe and efficient file operations while maintaining file attributes and permissions as needed.