Linux
  1. Linux文件和目录管理
  2. Linux打包和压缩
  3. Linux文本管理
  4. Linux用户和用户组
  5. Linux权限管理
  6. Linux进程管理
  7. Linux命令
    1. Linux stat命令:显示文件或文件系统的详细信息-7.1
    2. Linux man命令:显示联机帮助手册-7.2
    3. Linux info命令:info格式的命令帮助指令-7.3
    4. Linux whereis命令:査找二进制命令、源文件和帮助文档-7.4
    5. Linux which命令:列出命令的所在路径-7.5
    6. Linux find命令:在目录中查找文件-7.6
    7. Linux shutdown命令:关机和重启-7.7
    8. Linux ifconfig命令:查看和临时修改IP地址-7.8
    9. Linux ping命令:测试主机通信情况命令-7.9
    10. linux netstat命令:网络状态查看命令-7.10

Linux find命令:在目录中查找文件-7.6

程序员日记      2019-08-01

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逻辑非