Using rsync with path names containing spaces

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


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.