Jitsumi logo Jitsumi

Rsync Made an Extra Folder? Check the Source Slash Before Using --delete

Why rsync creates an extra directory, how a trailing slash changes the copy, and how to preview --delete before it removes files.

A terminal showing two rsync commands. Without a trailing slash the file lands in backup/project/README.txt; with the slash it lands in backup/README.txt.

TL;DR

  • A trailing slash on the rsync source copies its contents; without it, rsync copies the folder itself.
  • Preview every transfer with -ain and read the full destination paths before running it.
  • Treat --delete as a separate decision: check the dry run for *deleting lines first.

An rsync command finishes without an error, yet your backup now contains projects/projects/ instead of the files you expected. The transfer worked; the source directory was included one level deeper than you intended. The difference is often a single character.

A trailing slash on rsync's source directory means “copy this directory's contents”. Without it, rsync copies the named directory into the destination. Neither form automatically removes destination-only files. Adding --delete makes a wrongly chosen source or destination considerably more dangerous.

This guide is for Linux users copying projects to another folder or a mounted external drive. The local demonstrations below were executed with rsync 3.4.1 in disposable directories. They do not replace an application-aware backup or test remote transfers.

The folder-level difference

Imagine you have $HOME/work/project/README.txt, project/nested/notes.txt and a hidden project/.config/settings file. You already created /mnt/backup/projects/.

Comparison of two rsync commands. Without a trailing slash on the source, rsync copies the project folder itself, so README.txt lands in backup/project/. With a trailing slash it copies the folder's contents, so README.txt lands in backup/.

Compare these two commands:

rsync -a "$HOME/work/project" /mnt/backup/projects/
rsync -a "$HOME/work/project/" /mnt/backup/projects/

The first produces /mnt/backup/projects/project/README.txt. The second produces /mnt/backup/projects/README.txt. Both also transfer the hidden configuration folder. The rsync project's documented explanation of source trailing slashes describes this as “copy directory by name” versus “copy directory contents”.

A trailing slash on the destination means something different: it tells rsync to treat that path as a directory. It does not undo the extra directory level introduced by a source without a slash.

Command

Where README.txt lands

Intended outcome

rsync -a project backup/

backup/project/README.txt

Keep the source folder name.

rsync -a project/ backup/

backup/README.txt

Put the contents directly into backup.

rsync -a project/ backup/project/

backup/project/README.txt

Choose the destination folder explicitly.

rsync -a project backup/project/

backup/project/project/README.txt

Usually an unintended extra level.

Assume the destination directories shown already exist. Rsync can create missing directories in some circumstances, but relying on ambiguous paths is a poor backup habit. The single-item destination-name rules can produce especially confusing results when a destination path does not yet exist.

Before copying, verify the destination is really the drive

If your external disk should be mounted at /mnt/backup, check the mount itself. An ordinary directory may exist at that path even after the drive is unplugged. Copying into it could fill the system disk rather than the external drive.

On Linux, when /mnt/backup is supposed to be an actual mount point:

mountpoint -q /mnt/backup && echo "Backup volume mounted"
ls -ld -- "$HOME/work/project" /mnt/backup/projects

If the confirmation does not appear, stop and investigate. For a remote share or a different mount layout, check the real mount with an appropriate tool instead of assuming the path is safe because ls works.

Also decide whether projects/ should contain a directory named project or the files themselves. Write the expected full path of one sample file before choosing your command.

Preview the transfer without changing files

The -n switch tells rsync to perform a dry run; -i requests an itemised summary. -a selects archive mode (recursive copy plus commonly needed metadata preservation). Archive mode does not, by itself, mean versioned backup storage.

For an explicit destination folder holding the source contents, preview:

rsync -ain -- "$HOME/work/project/" /mnt/backup/projects/current/

You may see >f+++++++++ for a new regular file or cd+++++++++ for a new directory. Read the full resulting relative paths. If you expected current/README.txt, do not proceed when the preview suggests current/project/README.txt.

Rsync's dry-run documentation says the itemised operations should match a subsequent real run unless the data changes or an operation fails. A dry run cannot prove the destination will still be mounted later; recheck that immediately before transferring.

After verifying the preview, run the same command without -n:

rsync -ai -- "$HOME/work/project/" /mnt/backup/projects/current/

The -- signals the end of options for local path arguments. Keep both paths quoted. The approach transfers additions and updates but, with no deletion option, leaves unrelated destination files alone.

Why --delete deserves a separate decision

An rsync copy without --delete is not automatically an exact mirror. If the destination contains extra.txt and there is no corresponding source file, the file normally stays.

Adding --delete allows rsync to remove extra files in destination directories that are being synchronised. The upstream manual explicitly warns about mistaken deletions and recommends a dry run.

Suppose /mnt/backup/projects/current/ contains local-notes.txt, written only on the backup disk. Preview a mirror:

rsync -ain --delete -- "$HOME/work/project/" /mnt/backup/projects/current/

If local-notes.txt has no source counterpart, expect a line beginning *deleting. That is a warning worth reading, not a housekeeping detail. Stop if any deletion was unexpected. Do not use --delete merely to clean up the extra folder from a previous mistake: first check that folder for unique files and preserve them elsewhere.

For a strict one-way mirror where deletion is intentional, consider --max-delete=NUM as an additional cap, but only after reviewing a dry run and protecting irreplaceable data. The rsync documentation for --max-delete says rsync exits with an error when it exceeds the cap; backup automation must notice that error. A deletion cap does not compensate for a wrong destination.

A mirror also isn't a historical backup. If source data is deleted accidentally, that change can propagate to the mirror. Keep independent copies and test a restore, which a one-way mirror alone does not provide.

Run a harmless experiment in a temporary folder

You can reproduce the trailing-slash difference without copying your actual documents. These commands create a small lab using mktemp -d, transfer it twice and print the results:

LAB="$(mktemp -d)"
mkdir -p "$LAB/src/project/nested" "$LAB/by-name" "$LAB/by-contents"
printf 'hello\n' > "$LAB/src/project/README.txt"
printf 'note\n' > "$LAB/src/project/nested/notes.txt"
rsync -a "$LAB/src/project" "$LAB/by-name/"
rsync -a "$LAB/src/project/" "$LAB/by-contents/"
find "$LAB/by-name" -type f -print
find "$LAB/by-contents" -type f -print

The first listing should end with by-name/project/README.txt and by-name/project/nested/notes.txt. The other should end with by-contents/README.txt and by-contents/nested/notes.txt. Both copies hold identical sample file contents; the folder nesting differs.

On Linux with rsync 3.4.1, those two results were reproduced locally. We also tested a destination-only stale.txt: a no-delete run preserved it, while a --delete --dry-run printed *deleting stale.txt and left the actual file untouched. These tests use local static files, not an actively written database or a remote filesystem.

Inspect the temporary area before cleaning it up. Because the lab resides at an unpredictable temporary path, avoid copying a generic recursive removal command from an article into a shell with elevated privileges. Remove only the exact lab folder you just created, when you are certain it contains no work you want.

Troubleshooting the remaining surprises

The destination contains files I deleted from the source. That's expected without --delete. Decide whether you want to accumulate files, mirror a current state, or preserve restore points. Those are different jobs. Use a versioned backup system for restore points rather than expecting rsync's default copy to keep every prior version.

My wildcard copy skipped a hidden folder. project/* is expanded by your shell before rsync sees the arguments and may exclude dotfiles. Pass the directory itself, with or without the trailing slash depending on the desired layout. The manual's wildcard and deletion explanation also explains why passing individual matched files changes the scope of directory deletions.

An exclude rule affects what --delete removes. Basic exclusions normally protect matching destination items from deletion too; --delete-excluded changes that. If you use custom filter rules, test the exact command with -ain and inspect every deletion before running it. Treat filters as part of the operation, not as optional cosmetic flags.

A remote machine edited the same file. Rsync isn't a conflict-resolution tool. It follows the copy rules, which can replace a destination file. For competing edits across devices, compare the versions before choosing which one to keep.

Verify success after the transfer

Inspect the destination tree, open a few representative files and check a hidden directory if you rely on one. For critical static data, use a checksum comparison or a restore test appropriate to your application. Copying a live database while it is changing needs application-specific backup steps; rsync's success message alone does not prove consistency.

Remember three separate decisions: where the copied directory boundary should be, whether the destination should retain extra files, and whether you have a recoverable older version. The source slash controls the first. --delete controls part of the second. Neither substitutes for the third.

Subscribe

All the new articles in one Monday digest. Unsubscribe with one click.

Share this post

Newsletter

The Monday edit from Jitsumi

All the new articles, in one Monday email. Leave with one click whenever you like.