find-prune解析,


find-prune解析

gl@gl:~$ find ./ -name 'hello'

./info/hello

./hello

./temp/hello

gl@gl:~$ find ./ -path './temp' -prune

./temp

gl@gl:~$ find ./ -path './temp' -print

./temp

gl@gl:~$ find ./ -path './temp' -prune -a -print

./temp

gl@gl:~$ find ./ -path './temp' -prune -o -name 'hello' -print

./info/hello

./hello

gl@gl:~$ find ./ -path './info' -prune -o -name 'hello' -print

./hello

./temp/hello

gl@gl:~$ find ./ \ ( -path './temp' -o -path './info' \ ) -prune -o -name 'hello' -print

./hello

注意: '\ ('和'\ )'的左右两边都必须空格,且\和(之间是没有空格的。

如果想查找当前目录(/home/student)下的tmp.txt文件,但是想要避开sep目录:

?find /home/student -path /home/student/sep -prune -o -name "tmp.txt" -print

?

?sep后面不能加/ 即/home/student/sep/是错误的 如果当前目录为/home/student 也可以这样

?find . -path ./sep -prune -o -name "tmp.txt" -print

总结:-path 只是匹配find出来的路径,可以通过使用匹配符号* [] ?等 例如:

?

?[student@bioeng ~]$ find . -name file

./file

./dir/file

./dir/dir555/file

./dir/dir2/file

./dir/dir1/file

[student@bioeng ~]$

?

?[student@bioeng ~]$ find . -path "*dir[12]" -prune -o -name file -print

./file

./dir/file

./dir/dir555/file

?

?[student@bioeng ~]$ [student@bioeng ~]$ find . -path "*dir*" -prune -o -name file -print

./file

?[student@bioeng ~]$

对find参数-prune的理解

-prune就像一个判断语 句,当发现-prune前面的表达式math时,执行到-prune之后就会输出一个1结果,如果shell的话,

可以使用echo $?来看结果,如果-prune后面跟的是-o选项,用c语言的语法来讲的话就是1 || -print,所以明显可以看到

当-prune前面的 表达式成立的话,就不会执行-o后面的内容了,如果不成立,即0 || -print,那么将打印输出,

另外需要注意的是-path路径不能加入 结尾的/,

比如路径/vobs/gliethttp/signature,不能写成/vobs/gliethttp/signature/,这是 硬性规定

find /vobs/tmp/ -path /vobs/tmp/signature -a -print

如果find .那么后面-path的必须使用相对路径./gliethttp

除 find中.之外,其他所有查找,比如find tmp或者find /vobs等,-path都必须使用绝对路径

?先看下/mnt目录有什么东西:

~$ ls -l /mnt

总用量 0

-rwxr-xr-x 1 root root 0 2010-11-21 15:34 a

-rwxr-xr-x 1 root root 0 2010-11-21 15:34 a.txt

drwxrwxrwx 1 root root 0 2010-11-20 20:22shared

???再执行这个命令:

?

~$ find /mnt-path "/mnt/shared" -prune -o -print

/mnt

/mnt/a.txt

/mnt/a

?

?

???再执行这个命令:

?

~$ find /mnt -path "/mnt/shared" -prune

/mnt/shared

?

?

~$find /mnt -path "/mnt/shared" -prune -a -print

/mnt/shared

???再执行这个命令:

~$find /mnt -path "/mnt/shared" -prune -print

/mnt/shared

?

???为什么会有以上的不同呢?

???其实这个命令$ find /mnt -path "/mnt/shared" -prune -o -print要拆分成几段去理解。

???find /mnt -path "/mnt/shared"这个是最基本的find查找,查找目录为shared的,如果查找到,满足就返回真。

???如果加了 -prune选项,就表示,不寻找字符串作为寻找文件或目录的范本样式。

???-print选项的意思,假设find指令的回传值为True,就将文件或目录名称列出到标准输出。

???所以 find /mnt -path "/mnt/shared" -prune去进行查找,如果查找到dir1,find就返回true,-prune实际并没有起作用。?

???find /mnt -path "/mnt/shared" -prune -o -print,由于 -prune 和print是或的关系,如果find 返回真,首先执行prune,就被忽略了,没有输出;如果find返回假,然后执行prune,就为真的,然后print。

比如要在/usr/sam目录下查找不在dir1子目录之内的所有文件

?

find /usr/sam -path "/usr/sam/dir1" -prune -o -print

find [-path ..] [expression] 在路径列表的后面的是表达式

-path "/usr/sam" -prune -o -print 是 -path "/usr/sam" -a -prune -o -print 的简写表达式按顺序求值, -a 和 -o 都是短路求值,与 shell 的 && 和 || 类似如果 -path "/usr/sam" 为真,则求值 -prune , -prune 返回真,与逻辑表达式为真;否则不求值 -prune,与逻辑表达式为假。如果 -path "/usr/sam" -a -prune 为假,则求值 -print ,-print返回真,或逻辑表达式为真;否则不求值 -print,或逻辑表达式为真。

这个表达式组合特例可以用伪码写为

?

if -path "/usr/sam" then

?????????? -prune

else

?????????? -print

避开多个文件夹

?

find /usr/sam \( -path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -print

注意:圆括号()表示表达式的结合。即指示 shell 不对后面的字符作特殊解释,而留给 find 命令去解释其意义。由于命令行不能直接使用圆括号,所以需要用反斜杠'\'进行转意(即'\'转意字符使命令行认识圆括号)。同时注意'\(','\)'两边都需空格。

相关内容