Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

rsync Command Guide for Synchronizing Files and Directories

rsync copies files while comparing what already exists at the destination, so repeat runs transfer only what changed — making it the standard tool for backups, mirroring, and pushing files to servers.


What is rsync, and Why Use It?

rsync is a file synchronization tool: it copies from a source to a destination, but unlike cp, it first compares the two sides and sends only the differences. The first run copies everything; every run after that copies only what changed.

It works in three directions:

  • Local → local — mirror one folder onto another on the same machine.
  • Local → remote — push files to a server over ssh.
  • Remote → local — pull files down from a server.

Why Learn rsync?

  • Backups become cheap. A nightly backup of a 200 GB tree takes minutes after the first run, because unchanged files are skipped.
  • Interrupted transfers can resume instead of starting over from zero.
  • Attributes survive the copy: permissions, timestamps, symlinks, and ownership are preserved with -a.
  • It is the de facto admin standard — the way files reach production servers, day after day, without re-copying the world.

rsync is picky about trailing slashes: rsync -a src/ dst/ copies the contents of src, while rsync -a src dst/ creates dst/src. Get this wrong once and you’ll spend the rest of the afternoon untangling nested directories.


1. Synchronize Two Local Directories

rsync -av photos/ backups/photos/
  • -a / --archive is the workhorse flag: recursive, and it preserves symlinks, permissions, timestamps, owner, and group where possible.
  • Unchanged files are skipped; only new or modified files are copied.
  • The trailing slash on photos/ means “the contents of photos”. Without it, rsync would create backups/photos/photos/.

2. Preview Before You Commit (--dry-run)

rsync -avhn photos/ backups/photos/
  • -n / --dry-run prints what would happen and touches nothing.
  • Always run this first when the plan involves --delete, or when the destination is a directory you care about.

3. Watch Progress While It Runs

rsync -avh --progress photos/ backups/photos/
  • --progress prints a per-file progress bar and a summary at the end. For very large trees, --info=progress2 shows a single overall progress line instead of one bar per file.

4. Make the Destination an Exact Mirror (--delete)

rsync -av --delete photos/ backups/photos/
  • --delete removes files at the destination that no longer exist in the source — the destination becomes a byte-for-byte mirror.
  • Danger: a wrong source path plus --delete can wipe the destination. Test with -n first, and see section 6 for a safety net.

5. Copy to or from a Remote Server Over SSH

rsync -avz site/ [email protected]:/var/www/site/
rsync -avz [email protected]:/var/log/app/ ./app-logs/
  • Put a user@host: path on either side and rsync transports over ssh automatically — rsync must be installed on both ends.
  • -z / --compress compresses data in transit — worth it on slow links, wasted effort on a LAN.
  • For plain one-off copies, scp is simpler; for anything you’ll repeat, use rsync.

6. Keep a Safety Net (--backup)

rsync -av --delete --backup --backup-dir=../archive-$(date +%F) photos/ backups/photos/
  • --backup moves files that would be overwritten or deleted into --backup-dir instead of erasing them — the escape hatch for --delete.

7. Exclude What You Don’t Want to Sync

rsync -av --exclude='.git' --exclude='node_modules' project/ backup/project/
  • --exclude=PATTERN skips matching paths; pass it multiple times, or keep a list in a file with --exclude-from=FILE.

8. Never Overwrite Newer Files (--update)

rsync -avu src/ dst/
  • -u / --update skips files that are newer at the destination — handy when merging two folders where an old copy must not stomp a newer one.

9. Cap the Bandwidth

rsync -avz --bwlimit=1000 photos/ [email protected]:/backups/
  • --bwlimit=RATE caps throughput (in KB/s by default) so a big sync doesn’t saturate a shared connection.

10. Resume Interrupted Transfers

rsync -av --partial --progress huge.iso dst/
  • --partial keeps partially transferred files so the next run can pick up where it left off. Pair with --append-verify for a file that only ever grows.

11. Move Files Instead of Copying Them

rsync -av --remove-source-files incoming/ archive/
  • --remove-source-files deletes each source file after it has been copied successfully — rsync becomes a careful mv across directories or over the network. Empty directories are left behind; clean them up with find archive -type d -empty -delete.

Common Pitfalls

  • Trailing slash means contents; no slash means the directory itself. rsync -a src dst produces dst/src; rsync -a src/ dst puts the contents of src into dst. This is the number-one rsync mistake.
  • --delete is unforgiving. With a wrong path it deletes the destination. Dry-run (-n) first, and consider --backup.
  • rsync must exist on both ends for remote syncs. If the remote server doesn’t have it, the transfer fails before any bytes move.
  • By default, rsync decides “unchanged” from size and modification time. If you suspect silent corruption, add -c / --checksum to compare file contents instead.
  • On macOS the system rsync is ancient (2.6.9, missing --info and newer flags). Install a modern one with brew install rsync if you hit missing-option errors.

CommandDescription
cpLocal copies — everything, every time
scpSimple one-off copies over SSH
diffCompare files and directories
duCheck how big the sync will be
sshThe transport rsync rides for remote syncs

rsync Cheatsheet

CommandWhat It Does
rsync -av src/ dst/Archive-sync the contents of src into dst
rsync -n -av src/ dst/Dry run: show what would change
rsync -av --progress src/ dst/Sync with per-file progress bars
rsync -av --delete src/ dst/Make dst an exact mirror of src
rsync -av user@host:/path/ dst/Pull from a remote server
rsync -av src/ user@host:/path/Push to a remote server
rsync -av --exclude='*.log' src/ dst/Skip matching files
rsync -avu src/ dst/Never overwrite newer files
rsync -av --bwlimit=1000 src/ dst/Cap throughput at ~1 MB/s
rsync -av --partial src/ dst/Resume interrupted transfers