龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 操作系统 > LINUX系统 >

linux命令之find命令的12个常用参数详解(含具体用法和注意事项)(2)

时间:2014-05-20 02:26来源:网络整理 作者:网络 点击:
分享到:
4.使用find查找文件的时候怎么避开某个文件目录: 实例1:在test 目录下查找不在test4子目录之内的所有文件 命令: 代码如下: find test -path "test/test4" -pr

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
[root@localhost soft]#

5.使用user和nouser选项:
按文件属主查找文件:
实例1:在$HOME目录中查找文件属主为peida的文件
命令:

代码如下:
find ~ -user peida -print

实例2:在/etc目录下查找文件属主为peida的文件:
命令:

代码如下:
find /etc -user peida -print

说明:
实例3:为了查找属主帐户已经被删除的文件,可以使用-nouser选项。在/home目录下查找所有的这类文件
命令:

代码如下:
find /home -nouser -print

说明:
这样就能够找到那些属主在/etc/passwd文件中没有有效帐户的文件。在使用-nouser选项时,不必给出用户名; find命令能够为你完成相应的工作。

精彩图集

赞助商链接