Skip to content

rsync: The Smart File Transfer Tool

rsync is your go-to solution for efficiently copying files and directories. Unlike regular copy commands, rsync only transfers the parts of files that have changed, making it incredibly fast for subsequent transfers.

Terminal window
rsync -ahrvP source/ destination/

The Perfect Combination: -ahrvP

  • -a (Archive): Preserves everything important
    • File permissions
    • Ownership
    • Timestamps
    • Symbolic links
    • Special and device files
  • -h (Human-readable): Shows sizes in KB, MB, GB
  • -r (Recursive): Copies directories and their contents
  • -v (Verbose): Shows what’s happening
  • -P (Progress): Shows transfer progress and enables resume

Local Backups:

Terminal window
# Backup Documents folder
rsync -ahrvP ~/Documents/ /media/backup/Documents/
# Backup multiple directories
rsync -ahrvP ~/Documents/ ~/Pictures/ ~/Videos/ /media/backup/

Remote Transfers:

Terminal window
# Copy to remote server
rsync -ahrvP ~/local/files/ user@server:/remote/path/
# Copy from remote server
rsync -ahrvP user@server:/remote/files/ ~/local/path/
# Using custom SSH port
rsync -ahrvP -e "ssh -p 2222" ~/files/ user@server:/remote/path/

Smart Backups:

Terminal window
# Sync and delete extra files in destination
rsync -ahrvP --delete ~/source/ /backup/
# Exclude certain files
rsync -ahrvP --exclude '*.tmp' --exclude 'cache/' ~/source/ /backup/

Trailing Slashes Matter:

Terminal window
# With trailing slash - copies contents of 'source'
rsync -ahrvP ~/source/ /destination/
# Without trailing slash - copies 'source' directory itself
rsync -ahrvP ~/source /destination/

Dry Run First:

Terminal window
# Test what will happen without making changes
rsync -ahrvP --dry-run source/ destination/

Resume Interrupted Transfer:

Terminal window
# -P flag allows resuming if connection drops
rsync -ahrvP user@server:/large/file ~/local/
# If interrupted, run the same command to resume

Maintaining a Mirror:

Terminal window
rsync -ahrvP --delete source/ destination/

Daily Backup Script:

#!/bin/bash
rsync -ahrvP \
--exclude '*.tmp' \
--exclude 'node_modules/' \
~/important-files/ \
/backup/$(date +%Y-%m-%d)/

Remote Backup with Compression:

Terminal window
rsync -ahrvPz ~/large-files/ user@server:/backup/
# -z adds compression for slow connections
  • Use --dry-run before important transfers
  • Add extra v flags (-vv or -vvv) for more verbose output
  • Check permissions if transfers fail
  • Use --stats for detailed transfer statistics
  • Add --progress for individual file progress

Prerequisites:

  • rsync installed on both source and destination systems
  • SSH access for remote transfers
  • Sufficient disk space
  • Appropriate read/write permissions