使用name选项
文件名选项是find 最常用的选项,要么单独使用该选项,要么和其他选项一起使用。 可以使用某种文件名模式来匹配文件,记住要用引号将文件名模式引起来。不管当前路径是什么,如果想要在自己的根目录$HOME
中查找文件名符合*.log
的文件,使用~
作为 'pathname'
参数,波浪号~
代 表了你的$HOME
目录。
示例1:想要在当前目录及子目录中查找所有的‘ *.log‘文件
find . -name "*.log" -print
示例2:想要的当前目录及子目录中查找文件名以一个大写字母开头的文件
find . -name "[A-Z]*" -print
示例3:想要在/etc目录中查找文件名以host开头的文件
find /etc -name "host*" -print
示例4:想要查找$HOME目录中的文件
find ~ -name “*” -print 或find . -print
示例5:要想让系统高负荷运行,就从根目录开始查找所有的文件
find / -name "*" -print
示例6:想在当前目录查找文件名以一个个小写字母开头,最后是4到9加上.log结束的文件
:
find . -name "[a-z]*[4-9].log" -print
输出:
[root@localhost test]# ll
总计 316
-rw-r--r-- 1 root root 302108 11-13 06:03 log2012.log
-rw-r--r-- 1 root root 61 11-13 06:03 log2013.log
-rw-r--r-- 1 root root 0 11-13 06:03 log2014.log
-rw-r--r-- 1 root root 0 11-13 06:06 log2015.log
drwxr-xr-x 6 root root 4096 10-27 01:58 scf
drwxrwxr-x 2 root root 4096 11-13 06:08 test3
drwxrwxr-x 2 root root 4096 11-13 05:50 test4
[root@localhost test]# find . -name "[a-z]*[4-9].log" -print
./log2014.log
./log2015.log
./test4/log2014.log
用perm选项
按照文件权限模式用-perm选项,按文件权限模式来查找文件的话。最好使用八进制的权限表示法。
示例:在当前目录下查找文件权限位为755的文件,即文件属主可以读、写、执行,其他用户可以读、执行的文件
[root@localhost test]# find . -perm 755 -print
.
./scf
./scf/lib
./scf/service
./scf/service/deploy
./scf/service/deploy/product
./scf/service/deploy/info
./scf/doc
./scf/bin
还有一种表达方法:在八进制数字前面要加一个横杠-,表示都匹配,如-007就相当于777,-005相当于555
命令:
find . -perm -005
输出:
[root@localhost test]# ll
总计 316
-rw-r--r-- 1 root root 302108 11-13 06