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 let's 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 '[email protected]:/path/with spaces/' /local/path/

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

Write a Comment

Comment

39 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

  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. 🙂

  7. Unfortunately I continue to have issues with remote paths that include spaces. My local machine is Ubuntu 14.04 (rsync 3.1.0) and my remote is OS X 10.10 (rsync 3.1.1), both with bash as default shell. I have tried all variations, but nothing seems to work. Very frustrating!

    rsync [email protected]:/path/’with spaces/ /local/path/
    rsync ‘[email protected]:/path/with spaces/’ /local/path/
    rsync “[email protected]:/path/with spaces/” /local/path/
    rsync [email protected]:/path/”with spaces”/ /local/path/
    rsync [email protected]:/path/with spaces/ /local/path/
    rsync ‘[email protected]:/path/with spaces/’ /local/path/

    • Hey Marckr,

      I’m sorry, if what I posted doesn’t help I’m really not sure what to suggest. You might try asking a question on StackOverflow.com or ServerFault.com.

Webmentions

  • nicholas May 29, 2017
  • Backups in Mac OS X using rsync | joshvamos.com May 29, 2017
  • Malcolm Zachariah May 29, 2017