/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
| Command | Result |
|---|---|
| rsync -a src/ backup/ | backup/file1, backup/sub/file2 |
| rsync -a src backup/ | backup/src/file1, backup/src/sub/file2 |
| rsync -a src/ backup | Same as with backup/ (the destination slash does not matter) |
| rsync -a /data/src/ host:/srv/src/ | Mirror the contents into an existing remote directory |
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/file2Check before you copy
-n (--dry-run) with -v or -i (--itemize-changes) lists what rsync would do without changing anything:
rsync -avn src/ backup/Related options
| Option | Effect |
|---|---|
| -a, --archive | Recursive, and preserves permissions, times, symlinks, owner and group (owner needs root) |
| -n, --dry-run | Show what would be transferred, change nothing |
| --delete | Delete destination files that do not exist in the source |
| -R, --relative | Keep the full source path in the destination; /./ in the source marks where it starts |
| --exclude=PATTERN | Skip 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
Related pages
compare · rsyncrsync vs scp: which one to copy files over SSHrsync transfers only changed parts of files, resumes and mirrors directories with --delete; scp copies whole files every time. Both run over SSH. Differences.commands · tartar -xzf: extract a .tar.gz archiveExtract a .tar.gz archive with tar -xzf archive.tar.gz. Use -C to choose the target directory, -t to list contents first and -v to print each extracted file.commands · findfind -mtime +7: find and delete files older than N daysfind /path -type f -mtime +7 lists files modified more than 7 days ago; add -delete to remove them. How -mtime rounds days, -mmin, and safe -delete placement.commands · rsyncrsync --delete: mirror a directory and remove extra filesrsync -av --delete src/ dest/ mirrors a directory and removes files that are gone from the source. Preview with -n, keep backups, limit deletions, slash rules.