无法获取 rsync 排除选项来排除目录

发布于 2024-10-22 13:32:08 字数 299 浏览 2 评论 0原文

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

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

我可以让 rsync 排除任何隐藏文件。因此 '.*' 我无法获取要排除的排除目录。我尝试过使用 '=' 符号,用双引号和单引号将目录括起来。任何帮助将不胜感激。提前致谢。

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

ゝ偶尔ゞ 2024-10-29 13:32:08

实际上,既不是 Erik 的,也不是 Antoni 的答案是完全准确的。

埃里克说对了一半

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

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

从手册页:

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

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

因此,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排除 test/a/b /c/d (和子级)(test/a/ 锚定的点)。

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

Actually, neither Erik's nor Antoni's answer is fully accurate.

Erik is halfway right in saying that

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

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

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

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

Therefore, rsync -nvraL test/a test/dest --exclude=a/b/c/d will 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=/b/c/d, on the other hand, will exclude only test/a/b/c/d (and children) (test/a being the point to which / is anchored).

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

完美的未来在梦里 2024-10-29 13:32:08
mkdir -p test/a/b/c/d/e
mkdir -p test/dest
rsync -nvraL test/a test/dest --exclude=a/b/c/d 

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

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

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

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/a is the base directory synced from, the exclude pattern is specified by starting with 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.

旧故 2024-10-29 13:32:08

按照埃里克的例子,你想这样做:

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

Following Erik's example you want to do this:

rsync -nvraL test/a/ test/dest --exclude=/b/c/d
我很坚强 2024-10-29 13:32:08

其中任何一个都可以工作:

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

请注意,源路径中的结尾 / 对指定 --exclude 的方式有重要影响。这假设我们有:

mkdir -p test/a/b/c/d/e
mkdir -p test/a/other/place/a/b/c/d
mkdir -p test/dest

当排除路径以 / 开头时,原始帖子会遇到困难。 Daniel 的答案是正确的,排除路径中的初始 / 可能需要排除特定路径,并且这个初始的 / 应该被理解为正则表达式中的前导 ^ 。然而,他的答案对于源路径中的结尾 / 有一个严重的拼写错误。

Either of these would work:

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

Note the ending / in source path makes a critical difference to how --exclude should be specified. This assumes we have:

mkdir -p test/a/b/c/d/e
mkdir -p test/a/other/place/a/b/c/d
mkdir -p test/dest

Original Post has difficulty when exclude path starts with a /. Daniel’s answer is correct that this initial / in exclude path might be desirable to exclude a specific path, and that this initial / should be understood like leading ^ in regular expressions. However, his answer has a critical typo about the ending / in source path.

卷耳 2024-10-29 13:32:08

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

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

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

--exclude="$(realpath --relative-to=$PWD /home/file)"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文