文件查找
find
语法
find 路径 选项 参数 动作
动作(了解)
动作 |
含义 |
-print |
打印查找到的内容 |
-ls |
以长格式显示的方式打印查找到的内容 |
-delete |
删除查找到的文件(仅能删除空目录) |
-ok |
后面跟自定义shell命令(会提示是否操作) |
-exec |
后面跟自定义shell命令(标准写法-exec ;) |
按照文件名查找
-name
# 在根目录下找名字为zls的文件或者目录
[root@localhost ~]# find / -name zls
/var/spool/mail/zls
/home/zls
# 不区分大小写
[root@localhost ~]# find / -iname 1.txt
/root/1.TXT
/root/1.txt
/root/txt/1.txt
# 在根目录下查找包含txt后缀的文件或者目录
[root@localhost ~]# find / -iname "*.txt"
按照文件类型查找
-type
d 目录
f 文件 file
l 链接文件
s 套接字文件
p 管道文件
b 块设备 磁盘
c 块设备 设备(打印机)
# root下找文件
find /root -type f
# root下找目录
find /root -type d
# 根目录下找链接文件
find /root -type l
# 根目录下找套接字文件
find / -type s
# 根目录下找管道文件
find / -type p
# 根目录下找b块设备文件
find / -type b
# 根目录下找c块设备文件
find / -type c
**find中的或且非**
-a and 和
-o or 或
! nope 非
# 查找根目录下b块设备文件或者c块设备文件
find / -type b -o -type c
# 查找根目录下不是文件和目录的文件
find / ! -type f -a ! -type d
按照文件类型大小
-size
+5M 查找大于5M的
-5M 查找小于5M的
5M 查找等于5M的
# 查找根目录下小于5M 大于1M
find / -size -5M -a -size +1M
du -sh # 查看文件精准大小(人性化显示)
[root@localhost ~]# du -sh /usr/share/hwdata/iab.txt
1.7M /usr/share/hwdata/iab.txt
根据文件用户查找
-user
# 查找home下属主是ljy的
-group
# 查找home下属组是ljy的
# 查找home下属主是ljy或者属主是oldboyedu的
# 没有属主 属组
find / nouser
find / nogroup
根据时间查找
-mtime Modifide time 是在写入文件时随文件内容的更改而更改的
# 修改内容的命令都会刷新mtime 和 ctime
-atime Access time 是在读取文件或者执行文件时更新
# 类似 cat vim命令都会刷新atime
-ctime Change time 是在写入文件、更改所有者、权限或链接设置
# 修改内容的命令都会刷新mtime 和 ctime 只有修改文件属性的时候ctime才会单独刷新时间戳
## 按照mtime时间查找
# +n 查找n天之前的文件 不包含当天
find / -name '*.txt' -mtime +7
# -n 查找最近n天的文件 包含当天
find / -name '*.txt' -mtime -7
# n 查找不包含当天的,往前数第n天
find / -name '*.txt' -mtime 7
按照深度查找
-maxdepth
# 查找一级目录
find / -type d -maxdepth 1
# 查找二级目录
find / -type d -maxdepth 2
# 查找三级目录
find / -type d -maxdepth 3
根据文件权限查找
-perm
# 精确查找
例:
find ./ -perm 644
# 模糊匹配 -后面的对应权限
例:
find ./ -perm -222
# 模糊匹配 ugo中至少有rrw
find /home -perm /442
//包含set uid
[root@zls ~]# find /usr/sbin -perm -4000 -
ls
//包含set gid
[root@zls ~]# find /usr/sbin -perm -2000 -
ls
//包含sticky
[root@zls ~]# find /usr/sbin -perm -1000 -
ls