Skip to content

Linux cp Command: Complete File and Directory Copy Guide

Published: at 03:34 PMSuggest Changes

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

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

  1. Backup Operations

    # Backup configuration
    cp -a /etc/nginx/nginx.conf{,.backup}
    
    # Backup with date
    cp -a config.ini config.ini.$(date +%Y%m%d)
    
  2. Project Management

    # Copy project template
    cp -r template/ new_project/
    
    # Copy specific file types
    cp -r src/*.{js,css} dist/
    
  3. 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

  1. Preserve Attributes

    # Copy with all attributes
    cp -av source_dir/ dest_dir/
    
    # Copy with specific attributes
    cp --preserve=mode,ownership file1 file2
    
  2. Safe Copying

    # Interactive mode
    cp -i important.txt backup/
    
    # No clobber (don't overwrite)
    cp -n source.txt destination.txt
    
  3. Efficient Copying

    # Update only
    cp -ru source_dir/ backup_dir/
    
    # Copy with hard links
    cp -al source_dir/ snapshot_dir/
    

Best Practices

  1. Use Appropriate Options

    # For configuration files
    sudo cp -a /etc/config.conf{,.bak}
    
    # For user data
    cp -rp ~/documents/* ~/backup/
    
  2. Verify Copies

    # Use verbose mode
    cp -v file.txt backup/
    
    # Check with diff
    diff -r source_dir/ dest_dir/
    
  3. 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

  1. Permission Denied

    # Solution: Use sudo
    sudo cp restricted.txt /etc/
    
  2. No Space Left

    # Check space first
    df -h
    # Use compression
    tar czf - source_dir | (cd /dest && tar xzf -)
    
  3. File Exists

    # Solution: Use -i or -n
    cp -i source.txt destination.txt
    

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.


Previous Post
Linux mv Command: Moving and Renaming Files Guide
Next Post
Linux rm Command: Safe File and Directory Removal Guide