>_cmd.script

rsync

Efficiently synchronize files and directories

Networking

By CMD Script Team · 4 min read · Last updated

SYNTAX
rsync [OPTIONS] SOURCE DESTINATION

Options

Command options and flags
FlagDescription
-aArchive mode: recursive copy that preserves permissions, timestamps, symlinks, and ownership
-vVerbose mode, list files as they are transferred
-zCompress file data during transfer
--deleteDelete files in the destination that no longer exist in the source (mirror mode)
-nDry run: show what would be transferred without actually doing it
-eSpecify the remote shell to use, e.g. -e ssh or -e 'ssh -p 2222'
--progressShow per-file progress during the transfer
-PShorthand for --partial --progress, resuming and showing progress

Distribution compatibility

  • Ubuntu
  • Debian
  • Fedora
  • Arch
  • macOS

What it does

rsync synchronizes files and directories, either locally or over a network (typically tunneled through SSH), by comparing source and destination and transferring only the data that has actually changed. This delta-transfer approach makes it dramatically more efficient than a plain copy for repeated backups or deployments, and its --delete, --dry-run, and archive-mode options make it equally well suited to one-off mirroring and ongoing scheduled backup jobs.

Beginner examples

  • rsync -av ./src/ ./dest/ — copy the contents of src into dest, preserving permissions and timestamps
  • rsync -avz ./src/ user@host:/remote/dest/ — sync a local directory to a remote host over SSH with compression
  • rsync -av file.txt ./backup/ — copy a single file, preserving its attributes
  • rsync -n -av ./src/ ./dest/ — dry run: show what would change without copying anything
rsync -av ./notes/ ./notes-backup/

Advanced examples

  • Mirror a directory exactly, removing anything at the destination not present in the source: rsync -av --delete ./src/ ./dest/
  • Sync over SSH on a non-default port: rsync -avz -e "ssh -p 2222" ./src/ user@host:/remote/dest/
  • Resume and watch progress on a large transfer: rsync -avP ./bigfile.iso user@host:/data/
  • Exclude specific files or patterns from the sync: rsync -av --exclude='*.log' --exclude='node_modules/' ./app/ ./app-backup/
  • Preview a mirror operation before committing to deletions: rsync -anv --delete ./src/ user@host:/remote/dest/
rsync -avz --delete -e ssh ./dist/ deploy@server.example.com:/var/www/app/

Common mistakes

  • Getting the trailing slash wrong on the source path — rsync -a src/ dest/ copies the contents of src into dest, while rsync -a src dest/ (no trailing slash) copies src itself into dest, producing dest/src/ instead.
  • Adding --delete without first running a dry run (-n) to confirm what would be removed — accidental deletions at the destination are hard to notice until it's too late.
  • Assuming -a includes compression — it doesn't; add -z explicitly when transferring over a slow network link.
  • Forgetting that rsync needs to be installed on both ends for remote transfers over SSH, not just the machine initiating the sync.

Tips

  • Always pair --delete with -n (dry run) the first time you run a new mirroring command, to review exactly what would be removed.
  • Use --exclude (or --exclude-from=file) to skip build artifacts, .git, or node_modules directories that shouldn't be part of a sync or backup.
  • -P is shorthand for --partial --progress, which keeps partially transferred files around so an interrupted large transfer can resume instead of restarting from zero.

Best practices

  • Use -a (archive mode) as your default baseline for anything beyond trivial copies — it bundles recursion, symlink preservation, permissions, timestamps, and ownership into one flag.
  • Run a --dry-run before any --delete operation, especially in scripted or scheduled backup jobs, to avoid silently wiping data due to a path typo.
  • For scheduled backups, combine rsync with --link-dest to create space-efficient, hard-linked incremental snapshots instead of full copies each run.

Try it yourself

A simulated shell with a sample home directory — experiment freely, nothing leaves your browser. Type help to list supported commands.

Real-world use cases

  • Deploying a built static site or application bundle to a web server, transferring only changed files on each deploy.
  • Nightly incremental backups of a home directory or database dump directory to a remote backup host over SSH.
  • Mirroring large media or dataset directories between machines where re-copying everything each time would be prohibitively slow.

Common interview questions

  • Why is rsync generally preferred over scp/cp for repeated syncs? It uses a delta-transfer algorithm that only sends changed parts of files, making repeat syncs of large or mostly-unchanged directories far faster.
  • What does a trailing slash on the source path change? src/ syncs the contents of src into the destination; src (no slash) syncs the src directory itself, nesting it one level deeper at the destination.
  • How do you make rsync mirror a source exactly, including removing extra files at the destination? Add --delete, ideally verified first with a --dry-run (-n) pass to review what would be deleted.

Frequently Asked Questions

Why is rsync faster than scp for repeated transfers?

rsync uses a delta-transfer algorithm that compares source and destination and only sends the parts of files that actually changed, whereas scp always copies the entire file regardless of what's already there.

Why does a trailing slash on the source path matter so much?

rsync -a src/ dest/ copies the *contents* of src into dest; rsync -a src dest/ (no trailing slash on the source) copies the src directory itself into dest, creating dest/src/. Forgetting or adding a trailing slash is one of the most common rsync mistakes.

How do I make the destination an exact mirror of the source, including deletions?

Add --delete to remove files in the destination that no longer exist in the source: rsync -av --delete src/ dest/.

How do I preview what rsync will do before actually running it?

Add -n (or --dry-run), typically combined with -v, so rsync lists the changes it would make without touching any files: rsync -anv --delete src/ dest/.

Cheat sheet

Download a quick-reference cheat sheet for rsync.