Back to lessons

Dangerous Commands

Preview What Rsync Would Delete

You need to know which files rsync would delete before running a real sync.

Command

rsync -avhn --delete ./source/ ./backup/ | grep '^deleting'

What changed

Nothing changes because `-n` performs a dry run.

Danger

caution

When to use it

Use this before allowing rsync to delete anything.

When not to use it

Do not remove `-n` until paths and direction are confirmed.

Undo or recovery

No state is changed during the dry run.

Expected output

Lines showing files that would be deleted.

demo script

Disposable terminal steps

  1. find source backup -maxdepth 2 -type f | sort
  2. rsync -avhn --delete ./source/ ./backup/
  3. rsync -avhn --delete ./source/ ./backup/ | grep '^deleting'

simulated output

What it looks like

disposable vessel
::fixture-ready::
$ find source backup -maxdepth 2 -type f | sort
backup/app.conf
backup/stale.txt
source/app.conf
source/index.html
::exit-code::0
$ rsync -avhn --delete ./source/ ./backup/
sending incremental file list
deleting stale.txt
app.conf
index.html

sent 101 bytes  received 31 bytes  264.00 bytes/sec
total size is 24  speedup is 0.18 (DRY RUN)
::exit-code::0
$ rsync -avhn --delete ./source/ ./backup/ | grep '^deleting'
deleting stale.txt
::exit-code::0

YouTube Short

Preview rsync deletes first.

Before you let rsync delete files, dry-run it and isolate the delete lines.

LinkedIn hook

`rsync --delete` is useful. It is also how people erase the wrong side.

Question: Would you run `rsync --delete` without checking the delete list first?

experiments

A/B tests to run

Metric: youtube_retention_15s

A: Preview what rsync would delete.

B: This is the line I check before `rsync --delete`.