The rsync Trailing Slash Trap: One Character That Wipes Your Data
The #1 rsync mistake: a trailing slash plus --delete can erase your entire backup. Learn the difference between /path and /path/ before it's too late.
If rsync has a single footgun, this is it. One character — a trailing / — changes what gets copied. Combine that with --delete, and you can wipe your destination clean.
I’ve seen this happen in production twice. Both times the admin knew exactly what trailing slashes did — they just forgot which convention their last script used.
The difference
Without trailing slash — copies the directory itself:
rsync -av /srv/data /backup/
# Result: /backup/data/file1.txt, /backup/data/file2.txt
With trailing slash — copies the contents:
rsync -av /srv/data/ /backup/
# Result: /backup/file1.txt, /backup/file2.txt
Both are fine. The danger is switching without noticing.
Where it gets dangerous
Say your backup already has files from a previous run with the no-slash form:
/backup/data/file1.txt
/backup/data/file2.txt
Now you run the with-slash version plus --delete:
rsync -av --delete /srv/data/ /backup/
Data loss
rsync sees /backup/data/ as an “extra” directory that doesn’t exist in the source contents. With --delete, it removes the entire /backup/data/ tree — your previous backup is gone.
Safe habits
- Pick one convention and stick with it. Most admins prefer the trailing slash (copy contents). Just be consistent.
- Always dry-run before
--delete. Runrsync -avn --delete /srv/data/ /backup/first. No exceptions. Make it muscle memory. - Use
--backup-diras a safety net. Adding--backup-dir=/backup/.rsync-deleted/saves anything--deletewould remove, so you can recover from mistakes. - Make it an alias. Add this to your
.bashrcso dry-run is the default:
alias rsyncd='rsync -avn --delete'
# Usage: rsyncd /srv/data/ /backup/ (shows what WOULD happen)
Tip
If you use --delete without --dry-run first, you will eventually lose data. Not if. When.
For 30 more rsync commands organized by use case, see the complete rsync guide. And if trailing slashes are the kind of footgun that makes you want a GUI, rclone and Robocopy handle the source/destination distinction differently.