7

I mounted a disk at /data. This is empty at the moment.

I need rsync to perform mkdir -p than mkdir as the file I needs to be at at level 4 i.e. /data/dir_1/dir_2/dir_3/filename when dir_1, dir_2 and dir_3 doesn't exists.

If I create the needed sub-directories manually I am able to perform rsync using following command:

rsync -avz source_diretory/ /data/dir_1/dir_2/dir_3

But if I don't create the sub directories, this command throws an error:

mkdir: cannot create directory ‘/data/dir_1/dir_2/dir_3/’: No such file or directory

I tried Googling, reading rsync's man page, tried using -r -R but could not make it work.

Can rsync perform something like mkdir -p or is that out of its scope?

dragosrsupercool
  • 271
  • 1
  • 4
  • 11
  • See [this answer](https://stackoverflow.com/questions/1636889/rsync-how-can-i-configure-it-to-create-target-directory-on-server#22908437) – mivk Jul 28 '19 at 13:17

2 Answers2

8

rsync command doesn't create directory tree, so you can do this by perform:

 mkdir -p /data/dir_1/dir_2/dir_3

before rsync command or use --rsync-path options:

--rsync-path=PROGRAM Use this to specify what program is to be run on the remote machine to start-up rsync.

Your command should be:

 rsync -avz source_diretory/ --rsync-path="mkdir -p /data/dir_1/dir_2/dir_3 && rsync" /data/dir_1/dir_2/dir_3

Here is a useful link.

user.dz
  • 47,137
  • 13
  • 140
  • 258
Lety
  • 5,994
  • 2
  • 28
  • 36
0

A shorter way in Linux to create rsync destination paths is to use the '$_' Special Variable. (I think, but cannot confirm, that it is also the same in OSX).

'$_' holds the value of the last argument of the previous command executed. So the question could be answered with:

mkdir -p /data/dir_1/dir_2/dir_3 && rsync -avz source_directory/ $_

Dig
  • 331
  • 3
  • 5