rsync

Efficiently copy and synchronize files and directories locally or over the network.

What it does

rsync copies files and directories while transferring only the differences when possible. It is widely used for backups, mirroring, migrations, and keeping directories in sync.

How it works (mechanical)

Quick Start

# Copy one directory to another
rsync -av source/ destination/

# Copy to remote machine over SSH
rsync -av /home/user/ user@server:/backup/user/

# Dry run first
rsync -av --dry-run source/ destination/

Core Options (Most Useful Flags)

-a    Archive mode (preserve most metadata; recurse)
-v    Verbose
-h    Human-readable numbers
-z    Compress during transfer
--progress      Show transfer progress
--dry-run       Show what would happen, but do nothing
--delete        Delete files on destination not present in source
--exclude=PAT   Skip matching files
--include=PAT   Include matching files
-e ssh          Use SSH as remote shell
-n              Short form of --dry-run
-r              Recurse into directories
-u              Skip files newer on receiver
-P              Equivalent to --partial --progress

Very Important Trailing Slash Rule

rsync -av source/ dest/
# copies CONTENTS of source into dest

rsync -av source dest/
# copies the source DIRECTORY itself into dest

This is one of the most important things to understand in rsync.

12 Practical Examples

# 1) Sync one local directory to another
rsync -av /home/craig/docs/ /mnt/backup/docs/
# 2) Dry run before syncing
rsync -av --dry-run /home/craig/docs/ /mnt/backup/docs/
# 3) Show progress during copy
rsync -avh --progress /home/craig/videos/ /mnt/backup/videos/
# 4) Sync to remote server over SSH
rsync -av /home/craig/docs/ craig@server:/backup/docs/
# 5) Sync from remote server to local machine
rsync -av craig@server:/backup/docs/ /home/craig/restore/docs/
# 6) Compress during network transfer
rsync -avz /home/craig/data/ craig@server:/backup/data/
# 7) Delete files on destination that no longer exist in source
rsync -av --delete /home/craig/docs/ /mnt/backup/docs/
# 8) Exclude log files
rsync -av --exclude="*.log" /var/www/ /backup/www/
# 9) Exclude cache directories
rsync -av --exclude="cache/" /home/craig/project/ /backup/project/
# 10) Resume large interrupted transfer
rsync -avP bigfile.iso /mnt/backup/
# 11) Use a different SSH port
rsync -av -e "ssh -p 2222" /home/craig/docs/ craig@server:/backup/docs/
# 12) Sync only files newer in source
rsync -avu /home/craig/docs/ /mnt/backup/docs/

Very Useful Real-World Patterns

# Mirror a website directory
rsync -av --delete /var/www/html/ /backup/www/html/
# Backup home directory but skip caches and trash
rsync -av \
--exclude=".cache/" \
--exclude=".local/share/Trash/" \
/home/craig/ /mnt/backup/home-craig/
# Safer first step: dry run with delete
rsync -av --delete --dry-run /source/ /dest/
# Copy photos to external drive with progress
rsync -avhP ~/Pictures/ /run/media/craig/PHOTO_BACKUP/Pictures/

Notes & Gotchas

Safety Pattern

# SAFEST workflow:
# 1) dry run first
rsync -av --dry-run /source/ /dest/

# 2) if delete is involved, dry run that too
rsync -av --delete --dry-run /source/ /dest/

# 3) then do the real run
rsync -av /source/ /dest/

With rsync, previewing first is one of the best habits you can have.

Historical Context

rsync became popular because it could efficiently synchronize files by transferring only differences instead of copying everything every time. That made it especially valuable for backups and network transfers.

Modern Equivalent

Modern backup tools like restic, borg, and snapshot-based systems can do more, especially for versioned backups. But rsync remains one of the most trusted and flexible tools for straightforward sync and copy tasks.

Related Commands