rsync with remote filenames with spaces in from bash

Something that always annoys me with rsync is that due to executing a remote shell, any characters in the remote path name require double-escaping (once for this local shell, once for the remote one). For example

rsync -av 'my holiday photos/' server:'my holiday photos/'

creates a remote folder called ‘my’ and puts the directory into that. The solution is to do something like:

rsync -av 'my holiday photos/' server:'my\ holiday\ photos/'

But how to do this when you’re running from the shell eg iterating directories? One way would be to use a command like $(sed …) to handle the escaping, however you can do it purely in shell using two different types of quote. For example today I had to do:

for i in */; do
    rsync -av "$i/img/" server:"backup/'$i'/"

Leave a Reply

Your email address will not be published. Required fields are marked *