Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following loop in zsh:

 for x in my_folders*; 
   rsync -adrv $x/foo/bar/ /foo2/bar2/new_dir/$x/

new_dir does not exist before calling rsync, and I would like rsync to create it, along with everything else needed for the copy operation.

I am passing rsync the flags d and r but it complains with:

rsync: mkdir "/foo2/bar2/new_dir/item1" failed: No such file or directory (2)

It apparently assumes that the folders /foo2/bar2/new_dir/$x/ exist prior to the copying, which don't. With this:

  • Is there a way to ask rsync to create any necessary directories needed for the copy operation to work?
share|improve this question
up vote 4 down vote accepted

Looking at the rsync man page I don't see an option for your scenario. But what about adding the directory manually before starting the sync, thus avoiding your issue?

for x in my_folders*; 
   mkdir -p /foo2/bar2/new_dir/$x
   rsync -adrv $x/foo/bar/ /foo2/bar2/new_dir/$x/
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.