bash 文件名中的旧 rsync 和空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3331630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 22:23:21  来源:igfitidea点击:

old rsync and spaces in filenames

linuxmacosbashrsync

提问by Wells

Source directory is determined like so:

源目录是这样确定的:

SHOW=${PWD##*/}
[email protected]:"/mnt/bigfish/video/TV/${SHOW}/"

So it comes out something like:

所以它出来的东西是这样的:

[email protected]:/mnt/bigfish/video/TV/The Name Of the Show With Spaces/

Then trying to run rsync like so:

然后尝试像这样运行 rsync:

rsync -avz -e ssh "${SRC}" .

But it tells me that ""/mnt/bigfish/video/TV/The" is not a directory, ""/mnt/bigfish/video/TV/Name" is not a directory, etc, for however many space-delimited words are in the name of the source directory.

但它告诉我 ""/mnt/bigfish/video/TV/The" 不是目录,""/mnt/bigfish/video/TV/Name" 不是目录,等等,不管有多少空格分隔的词都在源目录的名称中。

How can I rectify this egregiously annoying issue?

我怎样才能纠正这个极其烦人的问题?

UPDATEI'm running this on OS 10.6, and I ended up string-replacing spaces with escaped spaces like so:

更新我在 OS 10.6 上运行它,我最终用转义空格替换了字符串,如下所示:

[email protected]:"/mnt/bigfish/video/TV/${SHOW// /\ }/"

回答by msw

From the rsync manual:

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

-s, --protect-args
此选项将所有文件名和大多数选项发送到远程 rsync,而不允许远程 shell 解释它们。这意味着空格不会在名称中拆分,并且不会翻译任何非通配符特殊字符(例如 ~、$、;、& 等)。通配符由 rsync 在远程主机上扩展(而不是由 shell 执行)。

回答by Coyote

As your question is dedicated to OS X, according to the Apple rsync manualyou can accomplish this using either simple quotes or the wildcard ?:

由于您的问题是针对 OS X 的,根据Apple rsync 手册,您可以使用简单的引号或通配符来完成此操作?

rsync -av host:'file\ name\ with\ spaces' /dest
rsync -av host:file?name?with?spaces /dest

Just had to do this and using the simple quotes works perfectly:

只需要这样做,使用简单的引号就可以完美地工作:

rsync -r --partial --progress --exclude=".cvs" --exclude=".svn" --exclude=".git" --rsh=ssh [email protected]:'/volume1/devel/__To\ SORT/___XXXXX\ Saves\ 2011-04' ./Saves2011

回答by Wrikken

This works:

这有效:

rsync -avz -e ssh "[email protected]:\"/mnt/bigfish/video/TV/${SHOW}/\""

So set:

所以设置:

[email protected]:\"/mnt/bigfish/video/TV/${SHOW}/\"

At least, here on Debian it works like a charm, no OS 10 available to test with here.

至少,在 Debian 上,它的作用就像一个魅力,这里没有可用于测试的 OS 10。

回答by William Casarin

You can do this on OSX if you're dealing with arguments in a script:

如果您在脚本中处理参数,则可以在 OSX 上执行此操作:

ESCAPED_SRC="$(echo "$SRC" | tr ' ' '\ ')"
ESCAPED_DEST="$(echo "$DEST" | tr ' ' '\ ')"
rsync -ravP "$ESCAPED_SRC" "$ESCAPED_DEST"