41

Example:

rsync /tmp/fol1/fol2/fol3/foln user@addr:/tmp/fol1/fol2/fol3/foln

My main problem is folder /tmp/fol1 doesn't exist on remote machine.

Which arguments can I use to force rsync to create this tree?

Devvyn
  • 87
  • 7
CrazySquirrel
  • 623
  • 1
  • 7
  • 9
  • 1
    Same question here: [rsync: how can I configure it to create target directory on server?](http://stackoverflow.com/questions/1636889/rsync-how-can-i-configure-it-to-create-target-directory-on-server) – mivk Oct 21 '16 at 11:31

4 Answers4

30

I ran into same issue today and found the solution here.

You can either do:

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

or:

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

to create /tmp/foo/bar/baz.c in the remote machine.

see --relative/-R section of man rsync for more details.

Anand Chitipothu
  • 4,167
  • 4
  • 24
  • 26
  • 2
    This is a very nice solution. But it does not work with the Cygwin port of rsync :( – greydet Feb 13 '14 at 15:38
  • this doesn't work if your source and destination paths aren't the same – Michael Apr 22 '20 at 20:39
  • what if i want to copy to root directory like this? ` rsync -avR somedir/./foo/bar/baz.c .` to copy from `/somedir/foo/bar/baz.c` to `/foo/bar/baz.c`. I couldnt make it work. Im not copying to remote. – sjd Oct 07 '20 at 09:29
23

One trick to do it is to use the --rsync-path parameter with the following value:

--rsync-path="mkdir -p /tmp/fol1 && rsync"

With --rsync-path you can specify what program is going to be used to start rsync in the remote machine and is run with the help of a shell.

barracel
  • 1,831
  • 13
  • 24
1

You can do this via bash and open a ssh tunnel make the file structure you need to make then rsync the data. Will these temp folders change each time this sync is done? eg is it for each day of the week?

ssh user@address
mkdir -p tmp/fol1
rsync avz /tmp/fol1/fol2/fol3/foln user@addr:/tmp/fol1/fol2/fol3/foln
fi
Paperghost
  • 96
  • 9
1

rysnc version 3.2.3 (6 Aug 2020) added the --mkpath option which achieves this purpose.

man rsync documents:

--mkpath                 create the destination's path component

Ubuntu 22.04 is the first Ubuntu version that will get this option as per: https://packages.ubuntu.com/search?keywords=rsync&searchon=names&suite=jammy&section=all

This had been previously mentioned in another answer to this question which got deleted, also mentioned at: rsync - create all missing parent directories?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985