Skip to content

/flags/rsync/rsync-trailing-slash

rsync trailing slash: rsync src/ dest vs rsync src dest

The commands on this page have not been executed by our verification runner yet.updated
tool
rsync
command
rsync
flag
SRC/ (trailing slash on the source)
default
no trailing slash: the directory itself is copied

A trailing slash on the source changes what rsync copies. rsync -a src/ dest/ copies the contents of src into dest. rsync -a src dest/ copies the directory itself, so you get dest/src/.... A trailing slash on the destination makes no difference. The rsync manual describes the slash as meaning "copy the contents of this directory" rather than "copy the directory by name".

The two forms

CommandResult
rsync -a src/ backup/backup/file1, backup/sub/file2
rsync -a src backup/backup/src/file1, backup/src/sub/file2
rsync -a src/ backupSame as with backup/ (the destination slash does not matter)
rsync -a /data/src/ host:/srv/src/Mirror the contents into an existing remote directory
bash
mkdir -p src/sub && touch src/file1 src/sub/file2
rsync -a src/ copy1/ && find copy1 -type f    # copy1/file1, copy1/sub/file2
rsync -a src  copy2/ && find copy2 -type f    # copy2/src/file1, copy2/src/sub/file2

Check before you copy

-n (--dry-run) with -v or -i (--itemize-changes) lists what rsync would do without changing anything:

bash
rsync -avn src/ backup/
OptionEffect
-a, --archiveRecursive, and preserves permissions, times, symlinks, owner and group (owner needs root)
-n, --dry-runShow what would be transferred, change nothing
--deleteDelete destination files that do not exist in the source
-R, --relativeKeep the full source path in the destination; /./ in the source marks where it starts
--exclude=PATTERNSkip matching files; patterns are relative to the transfer root

The same rule applies to cp -r in a different form: cp -r src dest creates dest/src only if dest already exists, which is why rsync's explicit slash is often preferred in scripts.

Sources