find 是 Linux 中强大的搜索命令,不仅可以按照文件名搜索文件,还可以按照权限、大小、时间、inode 号等来搜索文件。但是 find 命令是直接在硬盘中进行搜索的,如果指定的搜索范围过大,find命令就会消耗较大的系统资源,导致服务器压力过大。所以,在使用 find 命令搜索时,不要指定过大的搜索范围。
命令名称:find。
英文原意:search for files in a directory hierarchy.
所在路径:/bin/find。
执行权限:所有用户。
功能描述:在目录中查找文件。
按照文件名搜索
[root@localhost ~]#find 搜索路径 [选项] 搜索内容
选项:
-name: 按照文件名搜索;
-iname: 按照文件名搜索,不区分文件名大小;
-inum: 按照 inode 号搜索;
[root@VM_0_10_centos ~]# find / -name testzeo.txt
/test/testzeo.txt
find 命令是完全匹配的,必须和搜索关键字一模一样才会列出。
find 能够找到的是只有和搜索内容 test.txt 一致的 /test/test.txt 文件,而 /test/testzeo.txt等文件虽然含有搜索关键字,但是不会被找到。
[root@VM_0_10_centos test]# ls
test.txt testzeo.txt testzeo.txt.bak
[root@VM_0_10_centos test]# find / -name test.txt
/test/test.txt
按照文件大小搜索
[root@localhost ~]#find 搜索路径 [选项] 搜索内容
选项:
-size[ -]大小:按照指定大小搜索文件
这里的" "的意思是搜索比指定大小还要大的文件,"-" 的意思是搜索比指定大小还要小的文件
[root@VM_0_10_centos test]# ll -h
total 4.0K
-rw-r--r-- 1 root root 60 Aug 1 10:14 test.txt
-rw-r--r-- 1 root root 0 Aug 1 10:00 testzeo.txt
-rw-r--r-- 1 root root 0 Aug 1 10:08 testzeo.txt.bak
[root@VM_0_10_centos test]# find . -size -10k
.
./testzeo.txt.bak
./test.txt
./testzeo.txt
[root@VM_0_10_centos test]# find . -size -1k
./testzeo.txt.bak
./testzeo.txt
注意:搜索千字节必须是小写的"k",而兆字节必领是大写的"M"
按照修改时间搜索
Linux 中的文件有访问时间(atime)、数据修改时间(mtime)、状态修改时间(ctime)这三个时间,我们也可以按照时间来搜索文件。
[root@localhost ~]# find搜索路径 [选项] 搜索内容
选项:
-atime [ -]时间: 按照文件访问时间搜索
-mtime [ -]时间: 按照文改时间搜索
-ctime [ -]时间: 按照文件修改时间搜索
这三个时间的区别我们在 stat 命令中已经解释过了,这里用 mtime 数据修改时间来举例,重点说说 "[ -]"时间的含义。
-5:代表5天内修改的文件。
5:代表前5~6天那一天修改的文件。
5:代表6天前修改的文件。
[root@VM_0_10_centos test]# find . -mtime -5
.
./testzeo.txt.bak
./test.txt
./testzeo.txt
[root@VM_0_10_centos test]# find . -mtime 5
[root@VM_0_10_centos test]# find . -mtime 5
按照权限搜索
在 find 中,也可以按照文件的权限来进行搜索。权限也支持 [ /-] 选项
[root@localhost ~]# find 搜索路径 [选项] 搜索内容
选项
-perm 权限模式:査找文件权限刚好等于"权限模式"的文件
-perm -权限模式:査找文件权限全部包含"权限模式"的文件
-perm 权限模式:査找文件权限包含"权限模式"的任意一个权限的文件
[root@VM_0_10_centos test]# touch test.txt
[root@VM_0_10_centos test]# touch test1.txt
[root@VM_0_10_centos test]# touch test2.txt
[root@VM_0_10_centos test]# chmod -R 0755 test.txt
[root@VM_0_10_centos test]# chmod -R 0777 test1.txt
[root@VM_0_10_centos test]# chmod -R 0644 test2.txt
[root@VM_0_10_centos test]# ll
total 0
-rwxrwxrwx 1 root root 0 Aug 1 10:24 test1.txt
-rw-r--r-- 1 root root 0 Aug 1 10:24 test2.txt
-rwxr-xr-x 1 root root 0 Aug 1 10:24 test.txt
[root@VM_0_10_centos test]# find . -perm 755
.
./test.txt
[root@VM_0_10_centos test]# find . -perm 777
./test1.txt
[root@VM_0_10_centos test]#
按照所有者和所属组搜索
[root@localhost ~]# find 搜索路径 [选项] 搜索内容
选项:
-uid 用户 ID:按照用户 ID 査找所有者是指定 ID 的文件
-gid 组 ID:按照用户组 ID 査找所属组是指定 ID 的文件
-user 用户名:按照用户名査找所有者是指定用户的文件
-group 组名:按照组名査找所属组是指定用户组的文件
-nouser:査找没有所有者的文件
[root@VM_0_10_centos test]# find . -user root
.
./test2.txt
./test.txt
./test1.txt
按照文件类型搜索
[root@localhost ~]# find 搜索路径 [选项] 搜索内容
选项:
-type d:查找目录
-type f:查找普通文件
-type l:查找软链接文件
逻辑运算符
[root@localhost ~]#find 搜索路径 [选项] 搜索内容
选项:
-a:and逻辑与
-o:or逻辑或
-not:not逻辑非