On my local machine I have a folder structure like this


I want to rsync /a/c and /d/f to a remote server so that after which it has folder structure like


Do I have to call rsync twice or is there an option can automatically retain the folder structure?


Good question. You need to use the -R or --relative flag. From the man page:

-R, --relative Use relative paths. This means that the full path names specified on the command line are sent to the server rather than just the last parts of the filenames. This is particularly useful when you want to send several different directories at the same time. For example, if you used this command: rsync -av /foo/bar/baz.c remote:/tmp/ ... this would create a file named baz.c in /tmp/ on the remote machine. If instead you used rsync -avR /foo/bar/baz.c remote:/tmp/ then a file named /tmp/foo/bar/baz.c would be created on the remote machine, preserving its full path. These extra path elements are called “implied directories” (i.e. the “foo” and the “foo/bar” directories in the above example).


To preserve the directory structure, as above, use the -r or --recursive option (recurse into directories).

Then it is up to you how to make a seleciton. Use the -f, -F, --filter, --include or --exclude options. If needed, you can also use --inclide-from-file or --exclude-from-file, specially if you end up with more than one or 2 filters/includes/excludes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.