Preserve source paths when copying with rsync
Use archive mode for a directory tree and --relative to reproduce the desired source path at the destination.
Use rsync -a to copy a directory tree with its usual archive attributes. Add -R, or --relative, when you also want to recreate the source's leading path components under the destination.
These examples target upstream rsync. Check rsync --version: some operating systems ship a different implementation, such as openrsync, whose option behavior can differ. In particular, verify /./ relative-path handling with a temporary directory before relying on it.
Copy a directory or its contents
rsync -av /data/projects/ /backup/projects/
This copies the contents of projects into the named destination. When /backup/ already exists, these two forms differ:
# Include the projects directory itself.
rsync -av /data/projects /backup/
# Copy its contents directly into /backup/.
rsync -av /data/projects/ /backup/
Archive mode combines recursion, symlink preservation, permissions, modification times, group and owner handling, and device/special-file handling. Privileges and filesystem support still matter. It does not automatically include hard-link preservation, ACLs, or extended attributes; those use options such as -H, -A, and -X where supported.
Recreate the leading path
rsync -avR /home/alice/documents/reports/ /backup/
The destination contains home/alice/documents/reports/ beneath /backup/. To keep only part of that path, insert /./ at the desired boundary:
rsync -avR /home/alice/./documents/reports/ /backup/
Now the destination contains documents/reports/, without home/alice/. The dot component controls the transferred path; it is not an extra directory created at the destination.
For log collection from multiple hosts, give each host a separate destination:
rsync -avR -e ssh backup@host1:/var/log/./nginx/ /central-logs/host1/
The remote account needs read access to the selected logs. Copying actively written logs this way is not a durable event-delivery mechanism and can race with rotation.
Combine path preservation with filters
# Skip temporary files while preserving the requested path.
rsync -avR --exclude='*.tmp' /data/./projects/ /backup/
# Preview a mirror operation, including destination deletions.
rsync -avniR --delete /data/./projects/ /backup/
# Remote transfer with compression, when useful for the data.
rsync -avzR -e ssh /data/./projects/ backup@host2:/backup/
Review the dry-run's paths and deletion entries before removing -n. A mirror can delete destination-only data within the synchronized directory tree; it is not a versioned backup. Filter paths are relative to the transfer root, so changing a trailing slash or /./ boundary can also change which exclusion matches.
-a and -R are compatible: one governs attributes, the other path layout. Across hosts, normal owner/group mapping can use names. --numeric-ids preserves numeric IDs instead, which is appropriate only when those IDs mean what you intend at the destination.
Reference: rsync options and relative paths.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan