15

Suppose I have a directory on a local machine, behind a firewall:

local:/home/meee/workdir/

And a directory on a remote machine, on the other side of the firewall:

remote:/a1/a2/.../aN/one/two/
remote:/a1/a2/.../aN/one/dont-copy-me{1,2,3,...}/

...such that N >= 0.

My local machine has a script that uses rsync. I want this script to copy only one/two/ from the remote machine for a variable-but-known 'N' such that I end up with:

local:/home/meee/workdir/one/two/

If I use rsync remote:/a1/a2/.../aN/one/two/ ~/workdir/, I end up with:

local:/home/meee/workdir/two/

If I use rsync --relative remote:/a1/a2/.../aN/one/two/ ~/workdir/, I end up with:

local:/home/meee/workdir/a1/a2/.../aN/one/two/

Neither one of these is what I want.

  1. Are there rsync flags which can achieve the desired result?
  2. If not, can anyone think of a straightforward solution?
2

2 Answers 2

28

For -- relative you have to insert a dot into the source directory path:

rsync -av --relative remote:/a1/a2/.../aN/./one/two ~/workdir/

See the manual:

-R, --relative

[...]

It is also possible to limit the amount of path information that is sent as implied directories for each path you specify. With a modern rsync on the sending side (beginning with 2.6.7), you can insert a dot and a slash into the source path, like this:

             rsync -avR /foo/./bar/baz.c remote:/tmp/
7

If you are pulling files from an rsync older than v2.6.7, you can do this by taking advantage of rsync's --rsync-path option, which is meant to allow you to specify where the rsync command can be found on the remote. However, you can use it to change directory first. Simply,

rsync -aR --rsync-path='cd /a1/a2/.../aN/ && rsync' remote:one/two/ ~/workdir/

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.