C.W.K.
Stream
Lesson 07 of 12 · published

Dry Run & --delete Safety

~12 min · dry-run, delete, safety, backup

Level 0Pinger
0 XP0/101 lessons0/12 achievements
0/150 XP to next level150 XP to go0% complete

--dry-run is non-negotiable

-n (or --dry-run) shows exactly what rsync would do without doing it. Use it religiously, especially with anything destructive. The cost is one extra command; the upside is never accidentally deleting your work.

--delete makes destination a mirror

--delete removes files at the destination that don't exist at the source. This makes sync a true mirror operation. It's powerful and dangerous in equal measure — combined with the wrong trailing slash, it can wipe out the wrong directory in seconds.

Backup-and-delete: --backup-dir

If you want delete safety, use --backup --backup-dir=/some/path to move "deleted" files into a backup directory instead of removing them. They stick around long enough to recover from a mistake. Pair it with a dated subdirectory and you have a poor man's snapshot system.

Code

Dry run, then real, then safe-delete·bash
# 1. Dry run first
rsync -avzn --delete source/ dest/
# Read the output. Are the files-to-delete actually correct?

# 2. Real run only after the dry run looks right
rsync -avz --delete source/ dest/

# Safer: stash deletions in a backup-dir instead of removing
rsync -avz --delete \
  --backup --backup-dir=/tmp/rsync-bak-$(date +%Y%m%d) \
  source/ dest/

External links

Exercise

On a throwaway directory pair, deliberately set up source/ and dest/ with one file in dest/ that's not in source/. Run rsync -avzn --delete source/ dest/ — note the file rsync says it would delete. Now run with --backup --backup-dir=/tmp/bak instead — the file moves to /tmp/bak instead of vanishing. Internalize the safety pattern.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.