Linux 无法获得 rsync 排除选项以排除目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5343558/
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-08-05 03:18:17  来源:igfitidea点击:

Cannot get rsync exclude option to exclude dir

linuxbatch-fileunixscriptingrsync

提问by James P.

Having an issues with rsync. I'm using rsync as a glorified cp command. I have in a script the following code.

rsync 有问题。我使用 rsync 作为美化的 cp 命令。我在脚本中有以下代码。

rsync -aL --exclude /path/to/exclude/ --exclude='.*' /source/ /destination

rsync -aL --exclude /path/to/exclude/ --exclude='.*' /source/ /destination

I can get the rsync to exclude any hidden files. Hence the '.*'I cannot get the exclude dir to exclude. I've tried using an '='sign, surrounding the dir with double quotes, with single quotes. Any help would be greatly appreciated. Thanks in advance.

我可以让 rsync 排除任何隐藏文件。因此,'.*'我无法排除排除目录。我试过使用一个'='符号,用双引号和单引号包围目录。任何帮助将不胜感激。提前致谢。

采纳答案by Erik

mkdir -p test/a/b/c/d/e
mkdir -p test/dest
rsync -nvraL test/a test/dest --exclude=a/b/c/d 

This works. As test/ais the base directory synced from, the exclude pattern is specified by starting with a/

这有效。与test/a同步的基本目录一样,排除模式是通过以开头指定的a/

Show us the real paths/excludes if this doesn't help.

如果这没有帮助,请向我们展示真正的路径/排除。

Running rsync with -vn will list dirs/files - the pattern is matched against the format that rsync prints.

使用 -vn 运行 rsync 将列出目录/文件 - 该模式与 rsync 打印的格式相匹配。

回答by Antoni Villalonga

Following Erik's example you want to do this:

按照 Erik 的示例,您要执行此操作:

rsync -nvraL test/a/ test/dest --exclude=/b/c/d

回答by DanielSmedegaardBuus

Actually, neither of these are fully accurate.

实际上,这两种方法都不是完全准确的。

Erik is halfway right in saying that

Erik 说对了一半

As test/a is the base directory synced from, the exclude pattern is specified by starting with a/

由于 test/a 是同步的基本目录,因此排除模式以 a/ 开头指定

It is true that the exclude pattern's rootis test/a(i.e. the pattern /some/pathbinds to test/a/some/path), but that's not the whole story.

确实排除模式的test/a(即模式/some/path绑定到test/a/some/path),但这不是全部。

From the man page:

从手册页:

if the pattern starts with a / then it is anchored to a particular spot in the hierarchy of files, otherwise it is matched against the end of the pathname. This is similar to a leading ^ in regular expressions. Thus "/foo" would match a file named "foo" at either the "root of the transfer" (for a global rule) or in the merge-file's directory (for a per-directory rule).

如果模式以 / 开头,则它锚定到文件层次结构中的特定位置,否则它与路径名的末尾匹配。这类似于正则表达式中的前导 ^。因此,“/foo”将匹配位于“传输根”(对于全局规则)或合并文件的目录(对于每个目录规则)中的名为“foo”的文件。

We can ignore the per-directorybit as it doesn't apply to us here.

我们可以忽略这per-directory一点,因为它不适用于我们这里。

Therefore, rsync -nvraL test/a test/dest --exclude=a/b/c/dwill most definitely exclude test/a/b/c/d(and children), but it'll also exclude test/a/other/place/a/b/c/d.

因此,rsync -nvraL test/a test/dest --exclude=a/b/c/d绝对会排除test/a/b/c/d(和孩子),但它也会排除test/a/other/place/a/b/c/d.

rsync -nvraL test/a test/dest --exclude=/b/c/d, on the other hand, will exclude onlytest/a/b/c/d(and children) (test/abeing the point to which /is anchored).

rsync -nvraL test/a test/dest --exclude=/b/c/d,另一方面,将排除test/a/b/c/d(和孩子)(test/a作为/锚定的点)。

This is why you still need the anchoring inital slash if you want to exclude that specific pathfrom being backed up. This might seem like a minor detail, and it will be so the more specific your exclude pattern becomes (e.g. Picturesvs. home/daniel/Pictures) but it might just come around to bite you in the butt.

这就是为什么如果您想从备份中排除该特定路径,您仍然需要锚定初始斜杠。这可能看起来像是一个小细节,而且你的排除模式变得越具体(例如Picturesvs. home/daniel/Pictures),但它可能只是来咬你的屁股。

回答by qwr

If you want to specify absolute paths you can convert them to relative paths using realpath:

如果要指定绝对路径,可以使用realpath将它们转换为相对路径:

--exclude="$(realpath --relative-to=$PWD /home/file)"