Rsync – create all missing parent directories

rsyncscp

I'm looking for an rsync-like program which will create any missing parent directories on the remote side.

For example, if I have /top/a/b/c/d on one server and only /top/a exists on the remote server, I want to copy d to the remote server and have the b and c directories created as well.

The command:

rsync /top/a/b/c/d remote:/top/a/b/c

won't work because /tmp/a/b doesn't exist on the remote server. And if it did exist then the file d would get copied to the path /top/a/b/c.

This is possible to do with rsync using --include and --exclude switches, but it is very involved, e.g.:

rsync -v -r a dest:dir  \
  --include 'a/b'       \
  --include 'a/b/c'     \
  --include 'a/b/c/d'   \
  --include 'a/b/c/d/e' \
  --exclude 'a/*'       \
  --exclude 'a/b/*'     \
  --exclude 'a/b/c/*'   \
  --exclude 'a/b/c/d/*' 

will only copy a/b/c/d/e to dest:dir/a/b/c/d/e even if the intermediate directories have files. (Note – the includes must precede the excludes.)

Are there any other options?

Best Answer

You may be looking for

rsync -aR

for example:

rsync -a --relative /top/a/b/c/d remote:/

See also this trick in other question.