How to keep the full path with rsync?

Better Stack Team
Updated on November 23, 2023

When using rsync to back up data, you can preserve the full directory structure of your source data by using the -a (or --archive) option. The -a option is a shorthand for several other rsync options, including -r (recursive) and -l (preserve symlinks), among others. It ensures that the directory structure is maintained in the destination.

Here's the basic syntax for using rsync to back up data while preserving the directory structure:

rsync -av source_directory/ destination_directory/

In this command:

  • source_directory is the directory you want to back up.
  • destination_directory is the location where you want to store the backup.

The -a option ensures that the full directory structure, along with file attributes and permissions, is preserved. The trailing slash after source_directory/ is important; it tells rsync to copy the contents of the directory rather than the directory itself. This way, you'll have the contents of source_directory mirrored in destination_directory, maintaining the directory structure.

Here's a breakdown of some of the options used:

  • a (or -archive): Preserves a variety of file attributes and permissions, including recursive copying and symbolic link preservation.
  • v (or -verbose): Displays detailed information about the files being copied, which can be helpful for monitoring the backup process.

You can adjust the command to suit your specific needs, such as excluding certain files or directories using the --exclude option or specifying remote hosts for backup. The key to preserving the directory structure is to use the -a option, which ensures that the source directory's structure is maintained in the destination.

Got an article suggestion? Let us know
Explore more
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Make your mark

Join the writer's program

Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.

Write for us
Writer of the month
Marin Bezhanov
Marin is a software engineer and architect with a broad range of experience working...
Build on top of Better Stack

Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.

or submit a pull request and help us build better products for everyone.

See the full list of amazing projects on github