in Technology

Escaping Filename or Directory Spaces for rsync

To rsync a file or directory that contains spaces, you must escape both the remote shell and the local shell. I tried doing one or the other and it never worked. Now I know that I need to do both!

So lets say I’m trying to rsync a remote directory with my local machine and the remote directory contains a space (oh so unfortunately common with Windows files). Here’s what the command should look like:

rsync 'raam@example.com:/path/with\ spaces/' /local/path/

The single quotes are used to escape the space for my local shell and the backslash is used to escape the remote shell.

Write a Comment

Comment

32 Comments

  1. Thanks – I’ve been trying to work out how to do this for months! It never occurred to me to escape both the local and remote paths.

  2. or the rsync option –protect-args (-s), no slashes needed

    rsync manual describes how the remote shell interprets all filenames and options. with –protect-args the filenames and options are interpreted by the remote rsync only.

    no description in the manual how filenames and options are interpreted at the local shell by shell or rsync source sender. at least we know to write quoted filenames to rsync like most shell programs that read filenames with spaces.

    an rsync transfer, minimally escaped

    rsync -s andrew@example:/path/’path spaced’/ “/to/destination spaced/”

    for filenames, notice that you could either quote the spaced part of a path only, or the path full. either escape form may single quote or double quote

    • Thanks for the tip about –protect-args!

      This saved me a whole lot of trouble since although what Raam Dev wrote is correct it’s impossible (or very difficult) to use in scripts where the path with spaces is in a variable.

  3. Nice tip :)

    [trolling] While there is no such thing as ‘God’ -as those mind control systems known as religions wants us believe-, and while every Mac user on earth is a faggot[/trolling] I find your About section somewhat interesting and that’s a lot for me; I’ve already bookmarked your site to read it on my free time.

    Best

  4. This fixed my RSYNC problem on mac-os 10.8. I was copying my documents file to a windows network drive i created on my windows box using an external USB 3.0 hd.

    Here is the error:

    rsync error: some files could not be transferred (code 23)

  5. Thank you very much for this, I am not an expert so this saved me many years of hardship trying to find a solution!

  6. Thank you.
    Have had many folders in the form of “Fred & Charleen” for years under windows. Now in a mixed environment of Linux all appeared lost!

    • You’re welcome, Jim! I’ve grown to avoid spaces in filenames and directories like the plague. I immediately think about all the issues I might encounter if I need to deal with that file or directory on the command line. :)