How to rsync over ssh when directory names have spaces
Try
rsync --protect-args --size-only -avzPe ssh "/mnt/xlses/split/v2/name with space/ "[email protected]:/mnt/xlses/split/v2/name with space"
From man rsync
:
-s, --protect-args
This option sends all filenames and most options to the remote rsync without allowing the remote shell to interpret them. This means that spaces are not split in names, and any non-wildcard special characters are not translated (such as ~, $, ;, &, etc.). Wildcards are expanded on the remote host by rsync (instead of the shell doing it). [...]
This works in bash: Escape the spaces with backslash and then use quotes:
rsync -avuz [email protected]:"/media/Music/Heavy\ Metal/Witch\ Mountain/*" .
Or if you have the path in the variable $remote_path, spaces can be escaped with substitution:
rsync -avuz [email protected]:"${remote_path// /\\ }" .
Use two pairs of quotes
Don't bother with all the backslashes, just use single quotes inside double quotes:
ssh [email protected]:"'/home/me/test file'" .
You can also use the reverse, that is, double quotes inside single quotes:
ssh [email protected]:'"/home/me/test file"' .
Further info
Wildcards on the server side
If you want a *
to be interpreted on the server rather than the client, the *
must come inside just one of the 2 pairs of quotes. I find this counter-intuitive because logically the outer pair of quotes escapes the client interpretation, while the inner pair of quotes escapes the server interpretation.
Protect Args
The advantage, compared to the --protect-args
solution, is that you actually do not have the restrictions of the --protect-args
, so you can use special characters such as ~
or $
. So you can write:
rsync host:'"$HOME/test file"' .
or
rsync host:'~"/test file"' .
Note that the tilde (~
) in the latter example needs to be outside the double quotes.
You can put one of the pairs of quotes around the entire username@host:file part (e.g. ssh "[email protected]:'/home/me/test file'" .
)