How to handle spaces in paths with rsync on Linux?
Posted on In QAThe common rsync
commands seems not handle spaces well. For example,
rsync -avxP file "user@server:/data/my dir"
It reports:
rsync: link_stat "/home/zma/file" failed: No such file or directory (2)
How to make rsync handle spaces well?
You can use the --protect-args
option of rsync
.
$ rsync --protect-args -avxP file "user@server:/data/my dir"
What does --protect-args
do:
-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).
…
You may also control this option via the RSYNC_PROTECT_ARGS environment variable. If this variable has a non-zero value, this option will be enabled by default, otherwise it will be disabled by default.
Check rsync
manual for more details.