rsync

Powerful file synchronization and transfer tool for local and remote systems.

cli
rsyncsyncbackupsshtransfer

Basic Syntax

# Basic sync (source to destination)
rsync source destination

# Sync directory contents (note trailing slash)
rsync -av source/ destination/      # Sync contents of source/ into destination/
rsync -av source destination/       # Create source/ inside destination/

# Common options
rsync -av source/ dest/             # Archive mode + verbose
rsync -avz source/ dest/            # Add compression
rsync -avzh source/ dest/           # Human-readable sizes

Essential Options

# -a, --archive: Preserve permissions, timestamps, symlinks (recommended)
rsync -a source/ dest/

# -v, --verbose: Show detailed progress
rsync -av source/ dest/

# -z, --compress: Compress during transfer (good for slow connections)
rsync -avz source/ dest/

# -h, --human-readable: Display sizes in human-readable format
rsync -avzh source/ dest/

# -P, --partial --progress: Show progress + keep partial files
rsync -avP source/ dest/

# --delete: Delete files in dest that don't exist in source (mirror)
rsync -av --delete source/ dest/

# -n, --dry-run: Preview changes without executing
rsync -avn --delete source/ dest/

Remote Sync

# Upload to remote server (via SSH)
rsync -avz /local/path/ user@remote:/remote/path/

# Download from remote server
rsync -avz user@remote:/remote/path/ /local/path/

# Specify SSH port
rsync -avz -e "ssh -p 2222" /local/ user@remote:/remote/

# Sync between two remote hosts
rsync -avz user1@host1:/path/ user2@host2:/path/

Excluding Files

# Exclude specific file patterns
rsync -av --exclude='*.log' source/ dest/

# Exclude multiple patterns
rsync -av \
  --exclude='*.log' \
  --exclude='*.tmp' \
  --exclude='node_modules' \
  source/ dest/

# Exclude from file
rsync -av --exclude-from='exclude.txt' source/ dest/

# Include only specific patterns
rsync -av --include='*.jpg' --exclude='*' source/ dest/

Permissions and Ownership

# Preserve all attributes (permissions, ownership, timestamps)
rsync -a source/ dest/

# Preserve permissions only
rsync -p source/ dest/

# Preserve timestamps only
rsync -t source/ dest/

# Change ownership to current user
rsync -av --chown=USER:GROUP source/ dest/

# Don't preserve permissions (use dest defaults)
rsync -rltvz source/ dest/

Bandwidth and Performance

# Limit bandwidth (in KB/s)
rsync -av --bwlimit=1000 source/ dest/

# Skip files based on size
rsync -av --max-size='100M' source/ dest/    # Skip files larger than 100MB
rsync -av --min-size='1M' source/ dest/      # Skip files smaller than 1MB

# Partial transfers (resume interrupted transfers)
rsync -avP source/ dest/

# Skip files based on checksum (not just timestamp/size)
rsync -avc source/ dest/

Common Use Cases

Backup with Exclusions

# Backup home directory, excluding common unwanted files
rsync -av --delete \
  --exclude='.cache' \
  --exclude='.npm' \
  --exclude='node_modules' \
  --exclude='.Trash' \
  ~/Documents/ /backup/documents/

Mirror Website to Server

# Deploy website (delete removed files, compress, show progress)
rsync -avz --delete \
  --exclude='.git' \
  --exclude='node_modules' \
  ./dist/ user@server:/var/www/html/

Incremental Backup

# Create incremental backup with hard links (saves space)
rsync -av --delete \
  --link-dest=/backup/last-backup/ \
  /source/ /backup/current-backup/

Dry Run Before Sync

# Preview what would be changed (safe to test)
rsync -avn --delete source/ dest/

# Preview with detailed output
rsync -avin --delete source/ dest/

Progress and Logging

# Show detailed progress
rsync -av --progress source/ dest/

# Show per-file progress
rsync -av --info=progress2 source/ dest/

# Log to file
rsync -av --log-file=sync.log source/ dest/

# Verbose output with stats
rsync -av --stats source/ dest/

Special Cases

# Copy only directory structure (no files)
rsync -av -f '+ */' -f '- *' source/ dest/

# Sync only files newer than destination
rsync -avu source/ dest/

# Preserve hard links
rsync -avH source/ dest/

# Copy symlinks as symlinks (not their targets)
rsync -avl source/ dest/

# Delete files on destination during transfer (not after)
rsync -av --delete-during source/ dest/

# Ignore existing files (don't update)
rsync -av --ignore-existing source/ dest/

Safety Tips

# Always test with dry run first
rsync -avn --delete source/ dest/

# Use trailing slashes carefully
rsync -av source/ dest/    # Syncs contents of source into dest
rsync -av source dest/     # Creates source directory inside dest

# Backup before destructive operations
rsync -av --delete --backup --backup-dir=/backup/deleted/ source/ dest/

# Use --dry-run with --delete to preview deletions
rsync -avn --delete source/ dest/