2

I am trying to use rsync (on macOS Catalina) but have trouble with spaces in file path names. I have used -s or --protect-args but they do not do anything (when I look at the help for rsync, these options are not listed).

I am running rsync 2.6.9 in zsh. Can anyone help please?

Here is what I get when I run this rsync command:

chris@cgimac ~ % rsync -ahv /Volumes/G_5TB_general/Backup of CG-nas/mac drives/Fitness and health /Volumes/G_5TB_general/temp dump
building file list ... 
rsync: link_stat "/Volumes/G_5TB_general/Backup" failed: No such file or directory (2)
rsync: link_stat "/Users/chris/of" failed: No such file or directory (2) 
rsync: link_stat "/Users/chris/CG-nas/mac" failed: No such file or directory (2)
rsync: link_stat "/Users/chris/drives/Fitness" failed: No such file or directory (2)
rsync: link_stat "/Users/chris/and" failed: No such file or directory (2) 
rsync: link_stat "/Users/chris/health" failed: No such file or directory (2) done

I get the exact same result if I modify the command to

rsync  -ahv  ‘/Volumes/G_5TB_general/Backup of CG-nas/mac drives/Fitness and health’ ‘/Volumes/G_5TB_general/temp dump’

I have looked elsewhere and there are many examples eg st

https://www.cyberciti.biz/faq/rsync-transfer-filename-that-contains-whitespace/

where the -s (or --protect-args) option for rsync apparently work.

4
  • Can you please add the the actual rsync command you are running (including arguments) and the eror messages you get?
    – nohillside
    Commented Apr 9, 2020 at 5:05
  • rsync doesn't have a -s (Mojave at least), but -S. Also, no --protect-args either. Also, for spaces in file path, escape character works. Also, many commands accept strings contained in quotes.
    – anki
    Commented Apr 9, 2020 at 5:12
  • 1
    @ankii All commands do :-) Quoting is a shell mechanism, the command doesn't notice this.
    – nohillside
    Commented Apr 9, 2020 at 5:43
  • @nohillside I tried to be mostly correct ;)
    – anki
    Commented Apr 9, 2020 at 5:51

3 Answers 3

7

Though the example in question is a local rsync operation, where one level of argument quoting for the path is sufficient, rsync operations involving a remote server require two levels of quoting for the remote path. For example:

$ rsync -ahv \
    remoteserver:'/remote path w/ spaces and (special chars)' \
    '/local path w/ spaces and (special chars)'
bash: -c: line 0: syntax error near unexpected token `('
bash: -c: line 0: `rsync --server --sender -vlogDtpr . /remote path w/ spaces and (special chars)' .
...

$ rsync -ahv \
    remoteserver:\''/remote path w/ spaces and (special chars)'\' \
    '/local path w/ spaces and (special chars)'
receiving file list ...
...
  • The local path in the above rsync command is quoted only once (surrounded by ' quotes), because it is only interpreted by the local shell (as an argument to rsync).
  • The remote path in the above rsync command is quoted twice (surrounded by both ' quotes and \' literal quote characters), because it is interpreted by the local shell and the remote shell (as an argument to rsync --server).
  • An example with parentheses—in addition to spaces—was chosen for the illustrative error message, revealing why special characters are problematic when not properly quoted in the remote path.
  • In this example, the quoting is done in Bash for both the local shell and the remote shell. Depending on the quoting rules of other shells, it may need to be adapted.
  • Of course, if the path itself contains a ' character, you'll need to get fancier with the quoting, using something like How to escape quotes in the bash shell?.
4

The -s option doesn't protect against arguments with spaces passed on the command line, proper quoting does

rsync -ahv "/Volumes/G_5TB_general/Backup of CG-nas/mac drives/Fitness and health" "/Volumes/G_5TB_general/temp dump"

(in your second example you seem to use typographical quotes).

5
  • thanks for that, I copied your command line and tht worked.
    – chris
    Commented Apr 9, 2020 at 7:11
  • But when I added other option nothing happened and I got a "dquote" prompt
    – chris
    Commented Apr 9, 2020 at 7:12
  • rsync —-dry-run -ahv “/Volumes/G_5TB_general/Backup of CG-nas/mac drives/Fitness and health" "/Volumes/G_5TB_general/temp dump" dquote>
    – chris
    Commented Apr 9, 2020 at 7:12
  • @chris I have no idea what dquote> is, it's not a string which is included in /usr/bin/rsync. stackoverflow.com/questions/15773278/… might give a clue. Are you running this in the standard macOS Terminal or in another application?
    – nohillside
    Commented Apr 9, 2020 at 7:32
  • 3
    @chris - you are getting the dquote> prompt because your shell thinks you have a mismatched number of quotes and is prompting for another. In your example in the comments above you have three " - your command starts with rsync —-dry-run -ahv “ when it should start with rsync —-dry-run -ahv ". Take care if you use Textedit to format your commands - it sometimes likes to turn " into if you have smart quotes set - see Quotes problem in Mavericks (or TextEdit)
    – lx07
    Commented Apr 9, 2020 at 16:07
1

On mac OS I had to escape the first path normally, and the second path escaped + apostrophe. Example:

rsync -avhu VirtualBox\ VMs/Windows\ 10 [email protected]:"/Users/bob/VirtualBox\ VMs/"

This is the only combination that worked with spaces. Both machines are macs.

4
  • 1
    Why the downvote? If this doesn't work for you, try one of the other combinations 🤦‍♂️
    – lenooh
    Commented Aug 18, 2020 at 14:40
  • The „“ in the second part shouldn’t be necessary as long as you escape the space character
    – nohillside
    Commented Jan 6, 2021 at 20:17
  • 1
    I tried all combinations, and this is the only one that worked on MacOS. Both machines were macs.
    – lenooh
    Commented Jan 6, 2021 at 20:26
  • 1
    Weird to have to escape spaces inside of quotes, but this is the only thing that worked for me Commented Mar 24, 2021 at 3:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .