您的位置:首页 > 运维架构 > Linux

linux find prune exec 排除某目录或文件 执行

2013-12-17 13:57 239 查看
查找cache目录下不是html的文件

find ./cache ! -name '*.html' -type f

列出当前目录下的目录名,排除includes目录,后面的-print不能少

find . -path './includes' -prune -o -type d -maxdepth 1 -print

排除多个目录,”(“前是带”\”的

find / \( -path /home/ -o -path /root \) -prune -nouser -type f -exec ls -l {} \;

find查找文件的时候排除某个或几个文件或目录

比如要在/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 命令去解释其意义。


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

#find /usr/sam \(-path /usr/sam/dir1 -o -path /usr/sam/file1 \) -prune -o -name "temp" -print


linux下的常用命令find,加上不同的参数,可以使你很容易的找到需要的文件,但是有些时候,你在查找文件的同时,可能不需要在某文件夹下查找,这时候-prune就用上了。

比如在当前目录下寻找pl后缀的文件,不在scripts下寻找。

find . -path './scripts' -prune -o -name '*.pl' -print
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: