rsync
Efficiently synchronize files and directories
By CMD Script Team · 4 min read · Last updated
rsync [OPTIONS] SOURCE DESTINATIONOptions
| Flag | Description |
|---|---|
-a | Archive mode: recursive copy that preserves permissions, timestamps, symlinks, and ownership |
-v | Verbose mode, list files as they are transferred |
-z | Compress file data during transfer |
--delete | Delete files in the destination that no longer exist in the source (mirror mode) |
-n | Dry run: show what would be transferred without actually doing it |
-e | Specify the remote shell to use, e.g. -e ssh or -e 'ssh -p 2222' |
--progress | Show per-file progress during the transfer |
-P | Shorthand 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 ofsrcintodest, preserving permissions and timestampsrsync -avz ./src/ user@host:/remote/dest/— sync a local directory to a remote host over SSH with compressionrsync -av file.txt ./backup/— copy a single file, preserving its attributesrsync -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 ofsrcintodest, whilersync -a src dest/(no trailing slash) copiessrcitself intodest, producingdest/src/instead. - Adding
--deletewithout 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
-aincludes compression — it doesn't; add-zexplicitly 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
--deletewith-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, ornode_modulesdirectories that shouldn't be part of a sync or backup. -Pis 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-runbefore any--deleteoperation, especially in scripted or scheduled backup jobs, to avoid silently wiping data due to a path typo. - For scheduled backups, combine rsync with
--link-destto 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 ofsrcinto the destination;src(no slash) syncs thesrcdirectory 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.