find命令中的prune和or到底是怎么回事啊,弄了很长时间都没弄明白

系统安装、升级讨论
版面规则
我们都知道新人的确很菜,也喜欢抱怨,并且带有浓厚的Windows习惯,但既然在这里询问,我们就应该有责任帮助他们解决问题,而不是直接泼冷水、简单的否定或发表对解决问题没有任何帮助的帖子。乐于分享,以人为本,这正是Ubuntu的精神所在。
回复
头像
NewUserFF
帖子: 413
注册时间: 2010-11-19 22:55

find命令中的prune和or到底是怎么回事啊,弄了很长时间都没弄明白

#1

帖子 NewUserFF » 2012-04-13 15:29

执行命令:

代码: 全选

find . -path "./applications*" -prune -o -name "*.jpg" -print
结果:

代码: 全选

./application icons/wireshark.jpg
./86d6277f9e2f0708f73f26bfe924b899a901f253.jpg
./Wallpapers-room_com___The_Wood_Experiment_by_Delta909_1920x1200.jpg
把原来的那条命令后面的-print去掉后,结果:

代码: 全选

./application icons/wireshark.jpg
./applications
./86d6277f9e2f0708f73f26bfe924b899a901f253.jpg
./Wallpapers-room_com___The_Wood_Experiment_by_Delta909_1920x1200.jpg
疑问:
1.为什么两次加-print结果不一样?-print不是find自动加上的吗?按我的想法看加不加应该结果都是一样的,因为find都会自动把-print加上去
2.我明明在find命令中指明了,不要搜索形如./applications*的文件夹(prune的功能不就是不深入指定文件夹中吗),为什么结果中还是会显示这类文件呢

先谢谢大家了
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: find命令中的prune和or到底是怎么回事啊,弄了很长时间都没弄明白

#2

帖子 aerofox » 2012-04-13 17:11

第2个问题,注意那个 s,application icons 不匹配 applications*,所以列出来了。
头像
eexpress
帖子: 58428
注册时间: 2005-08-14 21:55
来自: 长沙

Re: find命令中的prune和or到底是怎么回事啊,弄了很长时间都没弄明白

#3

帖子 eexpress » 2012-04-13 17:17

If the expression contains no actions other than -prune, -print is per‐
formed on all files for which the expression is true.
所以不显示目录了。和-depth冲突。很少使用这-prune的。

多使用locate吧。
● type loc
loc 是 `locate -beLin 20' 的别名
● 鸣学
头像
b33e
帖子: 3874
注册时间: 2011-06-07 14:20

Re: find命令中的prune和or到底是怎么回事啊,弄了很长时间都没弄明白

#4

帖子 b33e » 2012-04-13 17:34

对,多用locate,find是折磨硬盘呢
头像
NewUserFF
帖子: 413
注册时间: 2010-11-19 22:55

Re: find命令中的prune和or到底是怎么回事啊,弄了很长时间都没弄明白

#5

帖子 NewUserFF » 2012-04-13 21:13

aerofox 写了:第2个问题,注意那个 s,application icons 不匹配 applications*,所以列出来了。
感觉很奇怪,加不加-print不是应该结果一样吗?我记得find好像是自动加-print的
头像
NewUserFF
帖子: 413
注册时间: 2010-11-19 22:55

Re: find命令中的prune和or到底是怎么回事啊,弄了很长时间都没弄明白

#6

帖子 NewUserFF » 2012-04-13 21:15

eexpress 写了:If the expression contains no actions other than -prune, -print is per‐
formed on all files for which the expression is true.
所以不显示目录了。和-depth冲突。很少使用这-prune的。

多使用locate吧。
● type loc
loc 是 `locate -beLin 20' 的别名
谢谢啦,不过我想排除某个目录进行搜索,google了一下,大部分答案都是关于这个-prune的,所以就学了一下,弄了半下午也没弄出个所以然来
aerofox
帖子: 1453
注册时间: 2008-05-24 8:30

Re: find命令中的prune和or到底是怎么回事啊,弄了很长时间都没弄明白

#7

帖子 aerofox » 2012-04-13 23:15

NewUserFF 写了:
aerofox 写了:第2个问题,注意那个 s,application icons 不匹配 applications*,所以列出来了。
感觉很奇怪,加不加-print不是应该结果一样吗?我记得find好像是自动加-print的
man find 写了:如果表达式没有包含 -prune 以外的动 作,当表达式为真时会执行 -print 动作。

-prune 如果没有给出 -depth 则返回 true; 不进入当前目录。
我想是这样的,当执行下面这条命令时,

代码: 全选

find . -path "./applications*" -prune -o -name "*.jpg"
遇到 -prune 虽然不进入 ./applications* 目录下,但是它本身是返回 true 的,这样就会执行 -print 动作,把这个目录显示出来。
而包含 -print 动作,这个动作只跟后面的 -name 作 -and 连接。
两者的区别是下面两条命令的区别:

代码: 全选

find . \( -path "./applications*" -prune -o -name "*.jpg" \) -print
find . -path "./applications*" -prune -o \( -name "*.jpg" \) -print
回复