find 一些常用参数的一些常用实例和一些具体用法和注意事项;
实例
实例一:查找根目录及其子目录下的所有文件名为 '*.log' 文件
xxxxxxxxxx11find ~ -name "*.log" -print实例二:查找当前目录及其子目录下的所有文件名为 '*.log' 文件
xxxxxxxxxx11find . -name "*.log" -print实例三:查找当前目录及其子目录下所有文件名以一个大写字母开头的文件
xxxxxxxxxx11find . -name "[A-Z]*" -print实例四:查找 /etc 目录下所有文件名以 host 开头的文件
xxxxxxxxxx11find /ect -name "host*" -print实例五:查找 $HOME 目录中的文件
xxxxxxxxxx11find ~ -name "*" -print实例六:想要让系统高负荷运行,就从根目录开始查找所有文件
xxxxxxxxxx11find ~ -name "*" -print实例七:在当前目录查找文件名以一个小写字母开头,最后是 4~9 加上 .log 结束的文件
xxxxxxxxxx11find . -name "[a-z]*[4-9].log" -printxxxxxxxxxx71Qs-MacBook-Pro:dir1 qiu$ ll2total 163drwxrwxr-x 3 qiu staff 96 4 22 10:35 log4-rw-r--r--@ 1 qiu staff 136 4 22 10:15 log.log5-rw-r--r-- 1 qiu staff 21 4 22 10:07 log1.log6Qs-MacBook-Pro:dir1 qiu$ find . -name "[a-z]*[0-9].log" -print7./log1.log按照文件权限模式用 -perm 选项,按文件权限模式来查找文件的话,最好使用八进制的权限表示法;
如在当前目录下查找文件权限为 755 的文件,即文件属主可以读、写、执行,其他用户可以读、执行的文件,可以用
xxxxxxxxxx71Qs-MacBook-Pro:dir1 qiu$ ll2total 163drwxrwxr-x 3 qiu staff 96 4 22 10:35 log4-rw-r--r--@ 1 qiu staff 136 4 22 10:15 log.log5-rw-r--r-- 1 qiu staff 21 4 22 10:07 log1.log6Qs-MacBook-Pro:dir1 qiu$ find . -perm 775 -print7./log还有一种表示法,在八进制数字前面要加一个横杆 - ,表示 权限至少是 ,如 -007 表示至少是 007 ,-005 表示至少是 005
xxxxxxxxxx121Qs-MacBook-Pro:dir1 qiu$ ll2total 163drwxrwxr-x 3 qiu staff 96 4 22 10:35 log4-rw-r--r--@ 1 qiu staff 136 4 22 10:15 log.log5-rw-r--r-- 1 qiu staff 21 4 22 10:07 log1.log6Qs-MacBook-Pro:dir1 qiu$ find . -perm -004 -print7.8./log.log9./.DS_Store10./log1.log11./log12./log/.DS_Store实例
在 dir1 目录下查找,但不希望在 dir1/log 目录下查找
xxxxxxxxxx11find dir1 -path "dir1/log" -prune -o -printxxxxxxxxxx161Qs-MacBook-Pro:dir1 qiu$ ls -lR dir12total 163drwxrwxr-x 5 qiu staff 160 4 23 10:44 log4-rw-r--r--@ 1 qiu staff 136 4 22 10:15 log.log5-rw-r--r-- 1 qiu staff 21 4 22 10:07 log1.log67dir1/log:8total 169-rw-r--r--@ 1 qiu staff 136 4 22 10:15 log.log10-rw-r--r-- 1 qiu staff 21 4 22 10:07 log1.log11Qs-MacBook-Pro:dir1 qiu$ find dir1 -path "dir1/log" -prune -o -print12dir113dir1/log.log14dir1/.DS_Store15dir1/log1.log16Qs-MacBook-Pro:dir1 qiu$ 实例一:在 dir1 目录下查找不在 log 目录下的所有文件
xxxxxxxxxx11// 同上一个实例!!说明
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,或逻辑表达式为真。
这个表达式组合特例可以用伪代码写为:
xxxxxxxxxx41if -path "test" then 2-prune 3else 4-print实例二:避开多个文件夹
xxxxxxxxxx11find dir1 \(-path dir1/test1 -o -path dir1/test2 \) -prune -o -printxxxxxxxxxx161// Mac_Terminal 下无效2[root@localhost soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -print3test4test/log2014.log5test/log2015.log6test/scf7test/scf/lib8test/scf/service9test/scf/service/deploy10test/scf/service/deploy/product11test/scf/service/deploy/info12test/scf/doc13test/scf/bin14test/log2013.log15test/log2012.log16[root@localhost soft]#说明
圆括号表示表达式的组合。\ 表示引用,即指示 shell 不对后面的字符串作特殊解释,而留给 find 命令去解释其意义;
实例三:查找某一确定文件,-name 等选项加在 -o 之后
xxxxxxxxxx11find test \(-path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -printxxxxxxxxxx61[root@localhost soft]# find test \( -path test/test4 -o -path test/test3 \) -prune -o -name "*.log" -print2test/log2014.log3test/log2015.log4test/log2013.log5test/log2012.log6[root@localhost soft]#按文件属主查找文件
实例一:在 $HOME 目录中查找文件属主为 peida 的文件
xxxxxxxxxx11find ~ -user peida -print实例二:在 /etc 目录下查文件属主为 peida 的文件
xxxxxxxxxx11find /etc -user peida -print实例三:为了查找属主账户已经被删除的文件,可以使用 -nouser 选项。在 /home 目录下查找所有的这类文件
xxxxxxxxxx11find /home -nouser -print和 user 一样,只是把 -user 改成 -group ,把 -nouser 改成 -nogroup
xxxxxxxxxx11find /app -group gem -printxxxxxxxxxx11find / -nogroup -print使用 -newer 选项;
实例一:查找更改时间比文件 log2012.log 更新,但比文件 log2017.log 更旧 的文件
xxxxxxxxxx11find -newer log2012.log ! -newer log2017.logxxxxxxxxxx181[root@localhost test]# ll2总计 3163-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log4-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log5-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log6-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log7-rw-r--r-- 1 root root 0 11-16 14:41 log2016.log8-rw-r--r-- 1 root root 0 11-16 14:43 log2017.log9drwxr-xr-x 6 root root 4096 10-27 01:58 scf10drwxrwxr-x 2 root root 4096 11-13 06:08 test311drwxrwxr-x 2 root root 4096 11-13 05:50 test412[root@localhost test]# find -newer log2012.log ! -newer log2017.log13.14./log2015.log15./log2017.log16./log2016.log17./test318[root@localhost test]#实例二:查找更改时间比 log2012.log 文件新的文件
xxxxxxxxxx11find . -newer log2012.log -printxxxxxxxxxx71[root@localhost test]# find -newer log2012.log2.3./log2015.log4./log2017.log5./log2016.log6./test37[root@localhost test]#在当前的文件系统中查找文件(不进入其他文件系统(windows 下为不进入其他盘,C盘下查询不查询D,E,F盘等));
实例一:在当前目录下查找位于本文件系统中文件名以 XC 结尾的文件
xxxxxxxxxx11find . -name "*.XC" -mount -print