DEV Community

ShellProven
ShellProven

Posted on Originally published at shellprovenai.github.io

rsync instead of cp: copy only what changed, dry-run first, and the trailing-slash trap

AI disclosure: this article was created with the help of AI. An AI assistant drafted the text from the lesson script, and it was reviewed before publishing. Every command was actually run in a fresh debian:13 container (rsync 3.5.0 (Debian 13)); all outputs below are copied from that run, not written by hand.

Use rsync instead of cp to keep a copy of a folder up to date: copy with -a, see exactly what changed with -i, preview deletions with a dry run (-n) before using --delete, learn the trailing-slash rule that decides whether you get a nested folder, and skip temporary files with --exclude.

The problem: Copying a folder with cp -r works once. The second time, it copies everything again, and it never removes files you deleted.

The first copy: rsync -a

Here's a photos folder: three pictures, some notes, a cache file and an old text file.

$ find photos -type f | sort
photos/2026/a.jpg
photos/2026/b.jpg
photos/2026/c.jpg
photos/cache/thumbs.tmp
photos/notes.txt
photos/old.txt
Enter fullscreen mode Exit fullscreen mode

rsync -a is archive mode: it copies folders recursively and keeps permissions and modification times. Note the slash after photos, we'll come back to it.

$ rsync -a photos/ backup/
Enter fullscreen mode Exit fullscreen mode
$ find backup -type f | sort
backup/2026/a.jpg
backup/2026/b.jpg
backup/2026/c.jpg
backup/cache/thumbs.tmp
backup/notes.txt
backup/old.txt
Enter fullscreen mode Exit fullscreen mode

Same files, same layout.

Only what changed: -i

Now let's change one file and run the same command again. -i prints one line for every file rsync actually transfers.

$ echo "Day 2: rain" >> photos/notes.txt && rsync -ai photos/ backup/
>f.st...... notes.txt
Enter fullscreen mode Exit fullscreen mode

Only notes dot t x t was sent. The letters say why: f for file, s for size, t for time. The three pictures were skipped, because their size and time didn't change. Run it once more, and there is nothing to do at all.

$ rsync -ai photos/ backup/ && echo "nothing to copy"
nothing to copy
Enter fullscreen mode Exit fullscreen mode

Deletions: dry run first

Now we delete the old file from the source. By default, rsync never deletes anything in the destination.

$ rm photos/old.txt && rsync -ai photos/ backup/ && ls backup
.d..t...... ./
2026
cache
notes.txt
old.txt
Enter fullscreen mode Exit fullscreen mode

The old file is still in the backup. --delete removes files that no longer exist in the source. That's powerful and dangerous: point it at the wrong folder and it deletes the wrong files. So always preview it first. -n is a dry run: rsync shows what it would do, and changes nothing.

$ rsync -ain --delete photos/ backup/
*deleting   old.txt
Enter fullscreen mode Exit fullscreen mode
$ ls backup
2026
cache
notes.txt
old.txt
Enter fullscreen mode Exit fullscreen mode

It would delete old dot t x t, and the file is still there. Only when the preview looks right, run it for real.

$ rsync -ai --delete photos/ backup/ && ls backup
*deleting   old.txt
2026
cache
notes.txt
Enter fullscreen mode Exit fullscreen mode

The trailing-slash trap

Here's the rule that catches everyone. With a slash after the source, rsync copies the contents of the folder.

$ rsync -a photos/ copy1/ && ls copy1
2026
cache
notes.txt
Enter fullscreen mode Exit fullscreen mode

Without the slash, it copies the folder itself, into the destination.

$ rsync -a photos copy2/ && ls copy2
photos
Enter fullscreen mode Exit fullscreen mode

One character, and your files land in copy two slash photos. Combined with --delete, a missing slash can make rsync remove everything it doesn't recognise. That's another reason to run the dry run first. The slash after the destination doesn't matter. Only the source slash changes the result.

Skip files with --exclude

The cache doesn't belong in a backup. --exclude takes a pattern, and the quotes stop the shell from expanding the star.

$ rsync -ai --delete --exclude='*.tmp' --exclude=cache/ photos/ clean/
created directory clean
cd+++++++++ ./
>f+++++++++ notes.txt
cd+++++++++ 2026/
>f+++++++++ 2026/a.jpg
>f+++++++++ 2026/b.jpg
>f+++++++++ 2026/c.jpg
Enter fullscreen mode Exit fullscreen mode
$ find clean -type f | sort
clean/2026/a.jpg
clean/2026/b.jpg
clean/2026/c.jpg
clean/notes.txt
Enter fullscreen mode Exit fullscreen mode

No cache folder and no temporary file. The same command works between machines, too: write the destination as user, at, host, colon, path, and rsync goes over SSH.

The trap, in one paragraph

Before running rsync with --delete for the first time, what should you add? -n shows every deletion and changes nothing. Run for real only when the list looks right.

Quick check

1. Which command copies only the contents of photos into backup: rsync -a photos backup/ or rsync -a photos/ backup/?

  • rsync -a photos backup/
  • rsync -a photos/ backup/

Answer
rsync -a photos/ backup/: The slash after the source means its contents. Without it, you get backup slash photos.

2. Before running rsync with --delete for the first time, what should you add?

  • -v
  • -n (a dry run)
  • --force

Answer
-n (a dry run): -n shows every deletion and changes nothing. Run for real only when the list looks right.

Recap

rsync -a copies a folder and keeps times and permissions, and every next run sends only what changed. -i shows what was sent. Preview with -n before --delete, put a slash after the source to copy its contents, and skip junk with --exclude.

Source and reproducible setup


This article was created with the help of AI and reviewed before publishing. The video on the lesson page uses AI narration and AI-generated robot-and-dog presenters. No sponsors, no affiliate links.

Top comments (0)