文章目录
find 命令
概述
find命令功能非常强大,通常用来在特定的目录下搜索符合条件的文件,也可以用来搜索特定用户属主的文件。
按文件名查询:使用参数 -name
命令:find + 路径 + -name +"文件名"
find /home -name "a.txt"
按文件大小查询:使用参数 -size
命令:find + 路径 + -size + 范围
-
范围
- 大于:+表示 -- +100k
- 小于:-表示 -- -100k
- 等于: 不需要添加符号 -- 100k
-
大小
- M 必须大写(10M)
- k 必须小写(20k)
例子: 查询目录为家目录
shell
# 等于100k的文件:
find ~/ -size 100k
#大于100k的文件:
find ~/ -size +100k
#大于50k, 小于100k的文件:
find ~/ -size +50k -size -100k
按文件类型查询:使用参数 -type
命令:find + 路径 + -type + 类型
- 类型
- 普通文件类型用 f 表示而不是-
- d -> 目录
- l -> 符号链接
- b -> 块设备文件
- c -> 字符设备文件
- s -> socket文件
- p -> 管道
测试
shell
#在指定目录下查找大于100K的文件
wuyou@itcast:~/share$ find /boot -size +100k
#在指定目录下查找小于100K的文件
wuyou@itcast:~/share$ find /boot -size -100k
#在指定目录下查找大小等于100K的文件
wuyou@itcast:~/share$ find /boot -size 100k
#查找根目录下文件大于10M
wuyou@itcast:~/share$ find / -size +10M
#查找根目录下文件小于10M
wuyou@itcast:~/share$ find / -size -10M
#查找根目录下文件等于10M
wuyou@itcast:~/share$ find / -size 10M
#查找根目录下头文件大于1M
wuyou@itcast:~/share$ find / -name "*.h" -size +1M
#查找boot目录下所有的grub.cfg文件, 并且使用ls -l显示
wuyou@itcast:~$ find /boot -name "grub.cfg" -exec ls -l {} \;
#在当前目录下查找txt文件,并且删除
wuyou@itcast:~/share$ ls
txt
wuyou@itcast:~/share$ find ./ -name "txt" -exec rm -rf {} \;
#在当前目录下查找txt文件,并且删除该文件,删除之前需要确认
wuyou@itcast:~/share$ find ./ -name "txt" -ok rm -rf {} \;
< rm ... ./txt > ? y
wuyou@itcast:~/share$
示例:
shell
find /etc -name grub.conf 查找/etc目录下的grub.conf文件
find / -name "*.conf" 查找/下所有.conf文件
find / -iname grub.conf 查找/目录下的grub.conf文件,忽略大小写
find / -maxdepth 2 -name grub.conf 可以使用-maxdepath参数来控制查找的层次,就是说只查当前目录和子目录,最多查2级目录
find / -mindepth 2 -name grub.conf 最少查二级目录
find /etc -type d 查找/etc/下所有的目录
find /etc -type f 查找/etc/下的所有普通文件
find /etc -type l -name *.conf 查找/etc/下软链接文件是.conf结尾的文件
find /etc -type s 查找/etc/下所有socket文件
find /etc -type c 查找/etc/下的所有字符设备文件
find /etc -type p 查找/etc/下所有管道文件
find /etc -user root 查找/etc/所属用户是root的文件
find /etc -group root 查找/etc/所属用户组是root的文件
find /etc -uid 500 查找/etc/下uid是500的文件,和-user类似
find /etc -gid 500 查找/etc/下gid是500的文件,和-group类似
find /etc -nouser 查找没有所属用户的文件
find /etc -nogroup 查找没有所属用户组的文件
find /etc -perm 777 -type d 查找/etc/下权限为777的目录
find . -perm 111 查找权限是111的文件
find . -size +10M 查找当前目录下大于10M的文件,单位可以有K,M,G,b等
find / -size -2M 查找根目录下少于2M的文件
find / -mtime 1 查找根目录下1天以前修改的所有文件
find / -mtime +2 查找根目录下2天以前修改的所有文件
find / -mtime -3 查找根目录下最近3天内修改的所有文件
find / -atime 1 查找根目录下1天以前访问或读过的所有文件
find / -atime -1 查找根目录下最近1天内读过或访问的文件
find / -ctime -3 查找根目录下最近3天内状态发生改变的文件
find / -cmin -3 查找根目录下最近3分钟内状态发生改变的文件
find / -empty 查找根目录下所有空白文件或者空目录
find / -false 查找根目录下总是错误的文件