C.W.K.
Stream
Lesson 03 of 08 · published

rsync: Smart File Sync

~13 min · rsync, sync, backup

Level 0Window Tourist
0 XP0/95 lessons0/14 achievements
0/100 XP to next level100 XP to go0% complete

The right tool for almost every copy

rsync copies files between two locations (local-local, local-remote, remote-remote) but only transfers what changed. It preserves permissions, handles symlinks, can resume after a network blip, and tells you exactly what it did.

The flag set you'll always type

rsync -avzP src/ dest/

  • -a — archive (recursion, perms, times, symlinks).
  • -v — verbose, list each file.
  • -z — compress over the wire.
  • -P — partial+progress (resumable, shows progress per file).

The trailing-slash rule

  • rsync -av src/ dest/ — copy contents of src into dest.
  • rsync -av src dest/ — copy src itself into dest, creating dest/src/.

Trailing slash on the source is the one decision you make before every rsync.

Useful extras

  • --delete — remove files in dest that aren't in src. Mirror mode. Use with care.
  • --exclude='node_modules' --exclude='.git' — skip patterns.
  • --exclude-from=.rsyncignore — read patterns from a file.
  • --dry-run (or -n) — show what would happen without doing it. Always run with -n first.
  • -e ssh — explicit transport (defaults to ssh nowadays).

Real patterns

  • Backup home: rsync -avzP ~/ /Volumes/backup/home/
  • Mirror to remote: rsync -avzP --delete ./site/ user@web:/srv/site/
  • Pull from remote: rsync -avzP user@server:/data/ ~/local-data/

Code

Always dry-run mirror first·bash
rsync -avzP --delete -n \
  --exclude='.DS_Store' --exclude='node_modules' \
  ./site/ user@web:/srv/site/
# Looks right? Drop the -n
rsync -avzP --delete \
  --exclude='.DS_Store' --exclude='node_modules' \
  ./site/ user@web:/srv/site/

External links

Exercise

Make a sandbox: mkdir -p /tmp/src/sub; touch /tmp/src/{a,b,c}.txt. Run rsync -avP -n /tmp/src/ /tmp/dst/. Read the dry-run output. Run without -n. Add a file to /tmp/dst; rerun with --delete -n to see it would be removed.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.