find命令关于忽略文件目录.转

回复 收藏
3.忽略某个目录:

如果在查找文件时希望忽略某个目录,因为你知道那个目录中没有你所要查找的文件,那么可以使用-prune选项来指出需要忽略的目录。在使用-prune选项时要当心,因为如果你同时使用了-depth选项,那么-prune选项就会被find命令忽略。如果希望在test目录下查找文件,但不希望在test/test3目录下查找,可以用:  

命令:

find test -path "test/test3" -prune -o -print

输出:

[root@localhost soft]# find test -path "test/test3" -prune -o -print

test

test/log2014.log

test/log2015.log

test/test4

test/test4/log2014.log

test/test4/log2013.log

test/test4/log2012.log

test/scf

test/scf/lib

test/scf/service

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

test/scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

[root@localhost soft]#

4.使用find查找文件的时候怎么避开某个文件目录:

实例1:在test 目录下查找不在test4子目录之内的所有文件

命令:

find test -path "test/test4" -prune -o -print

输出:

[root@localhost soft]# find test

test

test/log2014.log

test/log2015.log

test/test4

test/test4/log2014.log

test/test4/log2013.log

test/test4/log2012.log

test/scf

test/scf/lib

test/scf/service

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

test/scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

test/test3

[root@localhost soft]# find test -path "test/test4" -prune -o -print

test

test/log2014.log

test/log2015.log

test/scf

test/scf/lib

test/scf/service

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

test/scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

test/test3

[root@localhost soft]#

说明:

find [-path ..] [expression]

在路径列表的后面的是表达式

-path "test" -prune -o -print 是 -path "test" -a -prune -o -print 的简写表达式按顺序求值, -a 和 -o 都是短路求值,与 shell 的 && 和 || 类似如果

-path "test" 为真,则求值 -prune , -prune 返回真,与逻辑表达式为真;否则不求值 -prune,与逻辑表达式为假。如果 -path "test" -a -prune 为假,则求值 -print ,-print返回真,或逻辑表达式为真;否则不求值 -print,或逻辑表达式为真。

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

if -path "test" then  

-prune  

else  

-print  

实例2:避开多个文件夹:

命令:

find test \( -path test/test4 -o -path test/test3 \) -prune -o -print

输出:

[root@localhost soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -print

test

test/log2014.log

test/log2015.log

test/scf

test/scf/lib

test/scf/service

test/scf/service/deploy

test/scf/service/deploy/product

test/scf/service/deploy/info

test/scf/doc

test/scf/bin

test/log2013.log

test/log2012.log

[root@localhost soft]#



说明:

圆括号表示表达式的结合。  \ 表示引用,即指示 shell 不对后面的字符作特殊解释,而留给 find 命令去解释其意义。  

实例3:查找某一确定文件,-name等选项加在-o 之后

命令:

find test \(-path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print

输出:

[root@localhost soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print

test/log2014.log

test/log2015.log

test/log2013.log

test/log2012.log




传送门:http://www.cnblogs.com/peida/archive/2012/11/16/2773289.html
2015-08-04 11:00 举报
已邀请:
0

wzwyql

赞同来自:

学习了,不过这种表示方法。。。有点不能理解{:4_99:}
0

QuietM

赞同来自:

我试了下,,,只能显示目录文件,不能查找。。。。我也很郁闷。。。

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: