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


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


...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:


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


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


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 Answers 2


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/

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/

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .