rsync Cheatsheet - File Sync, Backup & --exclude Options
rsync is the default answer for mirroring trees because it transfers only what differs and can make the destination match the source exactly. That same power is the hazard: with --delete enabled, one mistaken rm on the source, or one mistaken mount, silently empties the destination. Preview every run with -n or --dry-run, and pair it with --itemize-changes so the actual list of transfers, creations and deletions is visible before you commit. The trailing slash is the other classic mistake: src/ copies the contents of the directory, src copies the directory itself, and the difference is one unwanted nesting level. This sheet groups parameters by transfer, display, filtering, backup and archive modes, clarifying what each of -a, -z, --partial and --checksum is actually for rather than listing them as interchangeable flags. Bandwidth matters on a fragile uplink, where --bwlimit and --inplace turn a mirror into something a slow link can survive overnight, and --delete is only safe once the source mount is known to be complete.
Basic Sync 4
rsync -avz ./src /destrsync -avz ./src/ /destrsync -avz user@host:/remote /localrsync -avz /local user@host:/remoteTransfer Options 5
-z, --compress-n, --dry-run--partial--bwlimit=1M-e sshDisplay Options 6
-v, --verbose-q, --quiet--stats-h, --human-readable--progress-PSkip & Filter 5
-u, --update-c, --checksum--exclude=.git--exclude-from=.rsyncignore--include=*.txtBackup Options 4
-b, --backup--suffix=.bak--backup-dir=/backup--deleteArchive Mode 7
-a, --archive-r, --recursive-l, --links-p, --perms-t, --times-g, --group-o, --ownerCommand Examples
Dry-run before deploying
rsync -avzn --delete ./dist/ user@host:/var/www/app/
# 确认输出无误后去掉 n-n 是 dry-run,最关键的一步。/dist/ 末尾的斜杠决定同步的是目录内容还是目录本身——写错会把文件多套一层。
Output
sending incremental file list index.html main.4f3a2b.js deleting old-vendor.js sent 1,204 bytes received 78 bytes 2,563.00 bytes/sec total size is 512,384 speedup is 39.08
Exclude what should not ship, and back up overwrites
rsync -avz --delete \
--exclude=".git" --exclude="node_modules" \
--backup --backup-dir=/var/backup/app \
./dist/ user@host:/var/www/app/--backup 配合 --backup-dir 把被覆盖的旧文件挪到指定目录,回滚有据可查。--delete 会删目标端多余文件,务必先 dry-run。
Resume a large transfer and cap bandwidth
rsync -avzP --bwlimit=1000 user@host:/data/bigfile.iso .-P 等于 --partial --progress,断了重连能续传。--bwlimit 单位是 KB/s,1000 即约 1MB/s,避免打满链路影响其他业务。
Output
sending incremental file list bigfile.iso 1,073,741,824 12% 12.00M 0:01:21 eta 0:01:10
Common Pitfalls
- The trailing slash on the source decides whether you sync the directory or its contents; omitting it creates an extra nesting level.
- --delete removes files absent from the source, so a wrong path is effectively a remote delete — always dry-run with -n first.
- rsync has no built-in backup; an overwritten file is replaced. Use --backup or tar the directory first for anything important.
- By default rsync compares size and mtime, so a touched-but-unchanged file is needlessly retransferred.
- Without -P an interrupted transfer leaves a truncated file that looks complete and will not be repaired on the next run.
Tips
- src/ syncs contents, src syncs the directory itself.
- -a is shorthand for -rlptgoD, preserving nearly all attributes.
- --delete removes extra target files; add -n to simulate first.
- rsync is incremental — only deltas are sent, very efficient.
- Remote transfer uses SSH by default; -e overrides the command.
Official References
Each command links to its official documentation below, so you can verify the latest usage and read deeper.
Maintained by LaoHand
Publicly updated on Jul 19, 2026, continuously proofread against official docs.
Contact Us
Wrong command or description? Send us corrections, business inquiries or product feedback by email.
Contact Us