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.

nohillside
  • 100,768
chris
  • 31
  • Can you please add the the actual rsync command you are running (including arguments) and the eror messages you get? – nohillside Apr 09 '20 at 05: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 Apr 09 '20 at 05:12
  • 1
    @ankii All commands do :-) Quoting is a shell mechanism, the command doesn't notice this. – nohillside Apr 09 '20 at 05:43
  • @nohillside I tried to be mostly correct ;) – anki Apr 09 '20 at 05:51

3 Answers3

6

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 single quote character, you'll need to get fancier using something like https://unix.stackexchange.com/a/187452.
mxxk
  • 275
3

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

nohillside
  • 100,768
  • thanks for that, I copied your command line and tht worked. – chris Apr 09 '20 at 07:11
  • But when I added other option nothing happened and I got a "dquote" prompt – chris Apr 09 '20 at 07: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 Apr 09 '20 at 07:12
  • @chris I have no idea what dquote> is, it's not a string which is included in /usr/bin/rsync. https://stackoverflow.com/questions/15773278/dquote-result-of-a-execution-a-program-in-linux-shell might give a clue. Are you running this in the standard macOS Terminal or in another application? – nohillside Apr 09 '20 at 07: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 Apr 09 '20 at 16:07
0

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

rsync -avhu VirtualBox\ VMs/Windows\ 10 bob@192.168.1.42:"/Users/bob/VirtualBox\ VMs/"

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

lenooh
  • 119