Mirror a Directory Incrementally with rsync on Unix
Copy only changed files and keep a destination directory in sync with rsync, verifying with dry-run before destructive deletes.
19 Jun 2026, 02:30 UTC

Problem and takeaway
You need to copy a directory tree to another location repeatedly, but transferring every file every time wastes time, network bandwidth, and disk I/O. You also want the destination to match the source exactly, which means removing files at the destination that no longer exist at the source.
Use rsync with archive mode and a trailing slash on the source path. Always preview with --dry-run before adding --delete, because that flag removes destination files missing from the source and rsync cannot undo it.
Desired outcome
After the procedure, the destination directory contains the same files, permissions, symlinks, and modification times as the source. Subsequent runs transfer only changed or new blocks.
Prerequisites
- A Unix-like system with
rsyncinstalled. Verify withcommand -v rsync. - Read access to the source directory and write access to the destination.
- For remote sync, SSH access to the destination host and rsync installed on both systems.
- Root or sudo privileges if you must preserve file ownership; non-root users may see "operation not permitted" for preserved metadata.
Understand the trailing-slash rule
The trailing slash on the source argument changes what gets copied:
| Invocation | Result |
|---|---|
rsync -a source/ dest/ | Copies the contents of source into dest. |
rsync -a source dest/ | Creates dest/source and copies the directory itself. |
Use source/ when you want dest to mirror the contents of source.
Procedure: local incremental sync
- Preview changes without modifying data. Run from your local shell:
- Run the synchronization. Remove
-n: - Check the exit code. Immediately after rsync finishes:
rsync -n -av --delete /path/to/source/ /path/to/destination/
Read the file list. Confirm that only intended files appear as transfers or deletions. The -n flag, also written --dry-run, performs no writes.
rsync -av --delete /path/to/source/ /path/to/destination/
Where to run: the host that can access both source and destination, typically your local machine or a job server. Required permissions: read on source, write on destination. Risk: --delete removes files at the destination that are missing at the source; a mistyped path can delete data.
echo $?
A result of 0 means success. Nonzero codes indicate syntax errors (1), protocol issues (2), or file-transfer errors (23 or 24).
Procedure: remote sync over SSH
Use SSH transport when the destination is on another host:
rsync -avz -e ssh /path/to/source/ user@backup-host:/path/to/destination/
The -z flag compresses stream data during transfer. Replace user@backup-host and paths with your environment. The remote host must run rsync; the local host initiates the connection over SSH.
Resume, throttle, and checksum options
- Resume partial transfers: add
--partial --progressto keep incomplete files and show transfer progress. - Limit bandwidth: add
--bwlimit=1000to cap throughput at roughly 1000 KB/s. - Validate integrity: add
--checksumto compare file checksums instead of relying on size and modification time. This reads every file on both sides and is slower.
Expected checks
- Inspect the dry-run output before the real run. File names listed under
deletingshould match files you expect to remove. - Run
echo $?after the command and verify it returns0. - Optionally compare a sample of files with
rsync --checksumorfindwithmd5sumon both sides.
Limitations
rsyncdoes not preserve extended attributes or ACLs unless-A(ACLs) and-X(extended attributes) are supported and used.- Non-root users may receive "operation not permitted" when preserving ownership; run as root or omit ownership preservation.
- This is a mirroring tool, not a versioned backup. Files deleted at the source are deleted at the destination when
--deleteis used. - The delta-transfer algorithm sends only changed blocks, but both sides generally benefit from rsync; local copies still avoid rewriting unchanged files.
Recovery if something goes wrong
Because --delete changes state, there is no rsync "undo." If you delete the wrong files, restore them from an existing backup or re-copy from a known-good source. Keep a separate snapshot or versioned backup before running destructive syncs on critical data.
Quick reference
| Flag | Meaning |
|---|---|
-a | Archive mode: recursion, symlinks, permissions, times, group, owner, devices. |
-v | Verbose output. |
-n | Dry-run; no changes are made. |
--delete | Remove extraneous files at the destination. |
-z | Compress data during transfer. |
-e ssh | Use SSH as the remote shell. |
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.