Bash:使用 -depth 和 -prune 进行查找以提供 cpio

发布于 2024-12-28 14:40:52 字数 663 浏览 3 评论 0原文

我正在构建一个备份脚本,其中某些目录不应包含在备份存档中。

cd /;
find . -maxdepth 2 \ 
    \( -path './sys' -o -path './dev' -o -path './proc' -o -path './media' -o -path './mnt' \) -prune \
-o -print

这只会找到我想要的文件和目录。

问题是 cpio 应提供以下选项,以避免恢复文件时出现权限问题。

find ... -depth ....

如果我添加 -深度 选项,返回的文件和目录将包含我想要避免的文件和目录。

我真的不明白查找手册中的这些句子:

-prune True;  if  the  file is a directory, do not descend into it. If
              -depth is given, false; no  effect.   Because  -delete  implies
              -depth, you cannot usefully use -prune and -delete together.

I'm building a backup script where some directories should not be included in the backup archive.

cd /;
find . -maxdepth 2 \ 
    \( -path './sys' -o -path './dev' -o -path './proc' -o -path './media' -o -path './mnt' \) -prune \
-o -print

This finds only the files and directories I want.

Problem is that cpio should be fed with the following option in order to avoid problems with permissions when restoring files.

find ... -depth ....

And if I add the -depth option, returned files and directories include those I want to avoid.

I really don't understand these sentences from the find manual:

-prune True;  if  the  file is a directory, do not descend into it. If
              -depth is given, false; no  effect.   Because  -delete  implies
              -depth, you cannot usefully use -prune and -delete together.

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

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

发布评论

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

评论(1

成熟稳重的好男人 2025-01-04 14:40:52

我引用本教程中的一段话,它可能会帮助您更好地理解 find-prune 选项。

了解如何防止 find 走得太远很重要。
在这种情况下,重要的选项是-prune。这个选项让人们感到困惑,因为它总是正确的。它有一个重要的副作用。如果正在查看的文件是一个目录,则它不会沿着该目录向下移动。下面是一个示例,列出了目录中的所有文件,但不查看顶层下子目录中的任何文件:

find * -type f -print -o -type d -prune

这将打印所有纯文件并删除所有目录中的搜索。要打印除源代码控制目录中的文件之外的文件,请使用:

find . -print -o -name SCCS -prune

如果排除 -o 选项,SCCS 目录将与其他文件一起打印。

来源

I am quoting a passage from this tutorial which might offer better understanding of -prune option of find.

It is important to understand how to prevent find from going too far.
The important option in this case is -prune. This option confuses people because it is always true. It has a side-effect that is important. If the file being looked at is a directory, it will not travel down the directory. Here is an example that lists all files in a directory but does not look at any files in subdirectories under the top level:

find * -type f -print -o -type d -prune

This will print all plain files and prune the search at all directories. To print files except for those in a Source Code Control Directories, use:

find . -print -o -name SCCS -prune

If the -o option is excluded, the SCCS directory will be printed along with the other files.

Source

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