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

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

Bash: find with -depth and -prune to feed cpio

bashfind

提问by Luis

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 cpioshould be fed with the following option in order to avoid problems with permissions when restoring files.

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

find ... -depth ....

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

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

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.

采纳答案by jaypal singh

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

我引用本教程其可能会提供更好的理解通路-prune的选择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 走得太远很重要。在这种情况下,重要的选项是-prune. 这个选项让人们感到困惑,因为它总是正确的。它有一个很重要的副作用。如果正在查看的文件是一个目录,则它不会沿着目录向下移动。这是一个示例,它列出了目录中的所有文件,但不查看顶级下子目录中的任何文件:

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.

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

Source

来源