我不太理解男士所给出的例子,谁能给我一些例子和解释?我可以在里面结合正则表达式吗?


更详细的问题是这样的:

写一个shell脚本changeall,它有一个类似changeall [-r| -r] "string1" "string2"的接口。它将找到后缀为.h、.c、.cc或.cpp的所有文件,并将所有出现的string1更改为string2。-r是只保留当前目录或包含子目录的选项。

注意:

对于非递归的情况,ls是不允许的,我们只能使用find和sed。 我尝试寻找深度,但它不支持。这就是为什么我想知道-prune是否可以帮助,但不理解从man find的例子。


EDIT2:我在做作业,我没有问太多细节,因为我想自己完成。既然我已经做完并交上来了,现在我可以陈述整个问题了。此外,我没有使用-prune就完成了作业,但无论如何我都想学习它。


当前回答

通常,我们在Linux中做事的原生方式,以及我们思考的方式,是从左到右的。

你可以先写下你想要的东西:

find / -name "*.php"

然后,您按下ENTER键,并意识到您从不想获取的目录中获取了太多文件。

因此,您认为“让我们排除/media以避免搜索挂载驱动器。”

现在你只需要将以下内容添加到前面的命令中:

-print -o -path '/media' -prune

最后一个命令是:

find / -name "*.php" -print -o -path '/media' -prune
|<--      Include      -->|<--      Exclude      -->|

我认为这种结构要简单得多,而且与正确的方法相关。

其他回答

修剪是一个“不递归此文件”开关(操作)。

从手册页

如果-depth未指定,则为true; 如果文件是一个目录,不要进入它。 如果给出了-depth,则为false;没有效果。

基本上它不会下降到任何子目录。

举个例子:

您有以下目录:

% find home
home
home/test1
home/test1/test1
home/test2
home/test2/test2

查找home -name test2将打印父目录和名为test2的子目录:

% find home -name test2
home/test2
home/test2/test2

现在,用-西梅…

找到home -name test2 -prune将只打印/home/test2;它不会进入/home/test2找到/home/test2/test2:

% find home -name test2 -prune
home/test2

显示所有内容,包括dir本身,但不显示其冗长乏味的内容:

find . -print -name dir -prune

如果你读了这里所有的好答案,我现在的理解是,以下都返回相同的结果:

find . -path ./dir1\*  -prune -o -print

find . -path ./dir1  -prune -o -print

find . -path ./dir1\*  -o -print
#look no prune at all!

但最后一个将花费更长的时间,因为它仍然搜索dir1中的所有内容。我想真正的问题是如何在不实际搜索的情况下删除不需要的结果。

所以我猜prune的意思是不要美化过去的比赛,而是把它标记为已经完成…

http://www.gnu.org/software/findutils/manual/html_mono/find.html “然而,这并不是由于‘修剪’动作的影响(它只是防止进一步下降,并不能确保我们忽略该项目)。相反,这种效果是由于使用了' -o '。由于./src/emacs的" or "条件左边已经成功,因此对于这个特定的文件,根本没有必要计算右边(' -print ')的值。"

注意,-prune并不像某些人说的那样防止下降到任何目录。它可以防止下降到与应用它的测试匹配的目录。也许一些例子会有所帮助(参见下面的正则表达式示例)。抱歉这么长时间。

$ find . -printf "%y %p\n"    # print the file type the first time FYI
d .
f ./test
d ./dir1
d ./dir1/test
f ./dir1/test/file
f ./dir1/test/test
d ./dir1/scripts
f ./dir1/scripts/myscript.pl
f ./dir1/scripts/myscript.sh
f ./dir1/scripts/myscript.py
d ./dir2
d ./dir2/test
f ./dir2/test/file
f ./dir2/test/myscript.pl
f ./dir2/test/myscript.sh

$ find . -name test
./test
./dir1/test
./dir1/test/test
./dir2/test

$ find . -prune
.

$ find . -name test -prune
./test
./dir1/test
./dir2/test

$ find . -name test -prune -o -print
.
./dir1
./dir1/scripts
./dir1/scripts/myscript.pl
./dir1/scripts/myscript.sh
./dir1/scripts/myscript.py
./dir2

$ find . -regex ".*/my.*p.$"
./dir1/scripts/myscript.pl
./dir1/scripts/myscript.py
./dir2/test/myscript.pl

$ find . -name test -prune -regex ".*/my.*p.$"
(no results)

$ find . -name test -prune -o -regex ".*/my.*p.$"
./test
./dir1/test
./dir1/scripts/myscript.pl
./dir1/scripts/myscript.py
./dir2/test

$ find . -regex ".*/my.*p.$" -a -not -regex ".*test.*"
./dir1/scripts/myscript.pl
./dir1/scripts/myscript.py

$ find . -not -regex ".*test.*"                   .
./dir1
./dir1/scripts
./dir1/scripts/myscript.pl
./dir1/scripts/myscript.sh
./dir1/scripts/myscript.py
./dir2

Find构建一个文件列表。它将您提供的谓词应用到每个谓词,并返回通过的谓词。

-prune的意思是排除在结果之外,这让我很困惑。你可以排除一个文件不修剪:

find -name 'bad_guy' -o -name 'good_guy' -print  // good_guy

-prune所做的只是改变搜索行为。如果当前匹配的是一个目录,它会说“嘿,找到你刚才匹配的那个文件,不要进入它”。它只是从要搜索的文件列表中删除该树(而不是文件本身)。

它应该被命名为-don - descent。