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.
rsyncis picky about trailing slashes:rsync -a src/ dst/copies the contents ofsrc, whilersync -a src dst/createsdst/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/--archiveis 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,rsyncwould createbackups/photos/photos/.
2. Preview Before You Commit (--dry-run)
rsync -avhn photos/ backups/photos/
-n/--dry-runprints 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/
--progressprints a per-file progress bar and a summary at the end. For very large trees,--info=progress2shows 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/
--deleteremoves 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
--deletecan wipe the destination. Test with-nfirst, 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 andrsynctransports oversshautomatically —rsyncmust be installed on both ends. -z/--compresscompresses data in transit — worth it on slow links, wasted effort on a LAN.- For plain one-off copies,
scpis simpler; for anything you’ll repeat, usersync.
6. Keep a Safety Net (--backup)
rsync -av --delete --backup --backup-dir=../archive-$(date +%F) photos/ backups/photos/
--backupmoves files that would be overwritten or deleted into--backup-dirinstead 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=PATTERNskips 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/--updateskips 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=RATEcaps 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/
--partialkeeps partially transferred files so the next run can pick up where it left off. Pair with--append-verifyfor a file that only ever grows.
11. Move Files Instead of Copying Them
rsync -av --remove-source-files incoming/ archive/
--remove-source-filesdeletes each source file after it has been copied successfully —rsyncbecomes a carefulmvacross directories or over the network. Empty directories are left behind; clean them up withfind archive -type d -empty -delete.
Common Pitfalls
- Trailing slash means contents; no slash means the directory itself.
rsync -a src dstproducesdst/src;rsync -a src/ dstputs the contents ofsrcintodst. This is the number-onersyncmistake. --deleteis unforgiving. With a wrong path it deletes the destination. Dry-run (-n) first, and consider--backup.rsyncmust exist on both ends for remote syncs. If the remote server doesn’t have it, the transfer fails before any bytes move.- By default,
rsyncdecides “unchanged” from size and modification time. If you suspect silent corruption, add-c/--checksumto compare file contents instead. - On macOS the system
rsyncis ancient (2.6.9, missing--infoand newer flags). Install a modern one withbrew install rsyncif you hit missing-option errors.
Related Commands
| Command | Description |
|---|---|
| cp | Local copies — everything, every time |
| scp | Simple one-off copies over SSH |
| diff | Compare files and directories |
| du | Check how big the sync will be |
| ssh | The transport rsync rides for remote syncs |
rsync Cheatsheet
| Command | What 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 |