1.find查找概述
很多时候我们忘记了文件所在的位置,此时就需要find
命令来查找
find 可以通过不同的条件来进行查找文件,比如:
文件名称、
文件大小、
文件时间、
属主属组、
权限等等
find 命令语法
命令 | 路径 | 选项 | 表达式 | 动作 |
---|---|---|---|---|
find | [ path ] | [options] | [expression] | [action] |
2 find查找示例
2.1 find基于名称查找 (-name)
#1、查找 /etc/ 目录下包含 ifcfg-ens32 名称的文件
[root@localhost ~]# find /etc/ -name "ifcfg-ens32"
#2、忽略大小写
[root@localhost ~]# find /etc/ -iname "ifcfg-ens32"
2.2 find基于大小查找(-size)
。+5M :大于5M
。-5M:低于5M
。5:等于5
[root@localhost ~]# find /etc -size +5M
[root@localhost ~]# find /etc -size 5M
[root@localhost ~]# find /etc -size -5M
2.3find 基于类型查找(- type)
#f 文件
[root@localhost ~]# find /dev -type f
#d 目录
[root@localhost ~]# find /dev -type d
#l 链接
[root@localhost ~]# find /dev -type l
#b 块设备
[root@localhost ~]# find /dev -type b
#c 字符设备
[root@localhost ~]# find /dev -type c
#s 套接字
[root@localhost ~]# find /dev -type s
#p 管道文件
[root@localhost ~]# find /dev -type p
2.4 find基于时间查找(-mtime)
![在这里插入图片描述](https://img-blog.csdnimg.cn/img_convert/e94296abbcd214773f7b3de2162dcc34.png#pic_center
#1、查找7天以前的文件(不会打印当天的文件)
[root@localhost ~]# find ./ -iname "file-*" -mtime +7
#2、查找最近7天的文件(会打印当天的文件)
[root@localhost ~]# find ./ -iname "file-*" -mtime -7
# 3、查找最近120分钟发生变化的文件
[root@localhost ~]# find ./ -type f -mmin -120
# 问题:查找/var/log下所有以 .log 文件结尾的文件,并且保留最近7天的log文件。
[root@localhost ~]# find /var/log -type f -name "*.log" -mtime +7 -delete
#将7天前的文件全部删除只剩7天后的文件,就保留下来了
2.5find基于用户查找
-user
属主查找
-group
属组查找
-nouser
没有属主查找
-nogroup
没有属组查找
2.6 find基于权限查找
-prem [/|-] mode
mode:精确权限匹配
-mode:每一类对象都必须同时拥有指定的权限(并且的关系,可包含)
/mode:任何一类(UGO)只要有一位匹配即可
2.7 find逻辑运算符
符号 | 作用 |
---|---|
-a | 与(并且) |
-o | 或(或者) |
-not或者! | 非(取反) |
3.find动作处理
查找到一个文件后,需要对文件进行如何处理,默认的动作是-print
动作|含义