How to handle spaces in paths with rsync on Linux?

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

Eric Ma

Eric is a systems guy. Eric is interested in building high-performance and scalable distributed systems and related technologies. The views or opinions expressed here are solely Eric's own and do not necessarily represent those of any third parties.

Leave a Reply

Your email address will not be published. Required fields are marked *