带空格的 SSH 路径上的 Rsync 不适用于引号

And*_*Riv 19 ssh rsync

只要路径中没有空格,我就可以通过 SSH 成功进行 RSYNC。

当路径确实有空格时,它不起作用。我试过斜线、引号和双引号。

当我使用斜杠时,输出表明它是成功的,但我没有看到任何传输的文件。

rsync -avz /path\ with\ spaces/ user@remotelocation:/media/another\ path\ with/spaces/
Run Code Online (Sandbox Code Playgroud)

当我使用单引号或双引号时,它告诉我输入密码后权限被拒绝

rsync -avz '/path with spaces/' 'user@remotelocation:/media/another path with/spaces/'
Run Code Online (Sandbox Code Playgroud)

我能做什么?

谢谢你。

小智 14

使用示例代码和参考扩展 rzr 的答案,只需添加-s标志,引用路径,而不必担心在远程路径中转义空格:

rsync -avzs '/path with spaces/' 'user@remotelocation:/media/another path with/spaces/'
Run Code Online (Sandbox Code Playgroud)

作为参考,OP 指定的选项:

  • -a,归档模式,等于 -rlptgoD(无 -H、-A、-X)
    • 包括:
    • -r, --recursive, 递归到目录
    • -l, --links, 将符号链接复制为符号链接
    • -p, --perms, 保留权限
    • -t, --times, 保留修改时间
    • -g, --group, 保留组
    • -o, --owner, 保留所有者(仅限超级用户)
    • -devices,保留设备文件(仅限超级用户)
    • -specials,保留特殊文件
  • -v, --verbose, 增加冗长
  • -z, --compress, 在传输过程中压缩文件数据

需要的附加参数:

  • -s, --protect-args, 没有空格分割,只有通配符


hee*_*ayl 13

您需要在本地 shell 和远程 shell 中转义空格。尝试这个:

rsync -avz '/path with spaces/' 'user@remotelocation:/media/another\ path\ with/spaces/'
Run Code Online (Sandbox Code Playgroud)

/path with spaces/本地shell中的源只能通过在其周围加上单引号来转义,即'/path with spaces/'.

另一方面,在目的地的情况下,本地 shell 通过放置单引号进行转义,而在远程 shell 中通过\在空格前面使用转义字符 ( ) 来对空格进行转义。

  • 只是为了强调 - 您需要引号和反斜杠。 (3认同)