一.简介
linux的find命令用于查找指定目录中符合参数条件的文件,如果不设置任何参数,则find命令将在当前路径下查找子目录与文件。并且将查到的子目录和文件全部显示。
二.find命令常用参数
1)根据文件名及文件拥有人拥有组查找文件
环境:
touch /westos/file{1..5}
useradd westos
chown student.student /westos/file1
chown student.westos /westos/file2
chown root.student /westos/file3
chown westos.westos /westos/file4
监控窗口:
watch -n 1 ls -l /westos/
1.find /etc -name passwd ---查找/etc下名称中带有passwd的文件
2.find /etc -maxdepth 2 -name passwd ---查找/etc目录下名称中带有passwd的文件,且只查找两层
find /etc -maxdepth 1 -name passwd ---查找/etc目录下名称为passwd的文件,且只向下查找一层
find /etc -maxdepth 2 -mindepth 2 -name passwd ---只在/etc下深度为2的目录中查找名称为passwd的文件
3.find /westos/ -user root ---查找/westos/下拥有人为root的文件
![](https://img-blog.csdnimg.cn/20181114180931846.png)
find /westos/ -group root ---查找/westos/下拥有组为root的文件
![](https://img-blog.csdnimg.cn/20181114180937499.png)
find /westos/ -user root -group student ---查找/westos/下拥有人为root,拥有组为student的文件
![](https://img-blog.csdnimg.cn/20181114180943950.png)
find /westos/ -not -user student ---查找/westos/下拥有者不是student的文件
![](https://img-blog.csdnimg.cn/20181114180951170.png)
find /westos/ -user student -o -group westos ---查找/westos/下拥有人是student或者拥有组为westos的文件,不加参数-o表示并且
2)根据文件的大小,文件类型及创建时间查找
监控窗口:
dd if=/dev/zero of=file1 bs=1 count=10240 ---文件/westos/file1大小为10M
dd if=/dev/zero of=file1 bs=2 count=20480 ---文件/westos/file2大小为20M
dd if=/dev/zero of=file1 bs=3 count=40960 ---文件/westos/file3大小为40M
watch -n 1 ls -l /westos/
实验窗口:
1.find -size 20k ---查找本地大小为20k的文件
![](https://img-blog.csdnimg.cn/20181114182522848.png)
find -size -20k ---查找本地文件小于20k的文件
![](https://img-blog.csdnimg.cn/20181114182530653.png)
find -size +20k ---查找本地文件小于20k的文件
2.find /westos -type d ---查找/westos下文件类型为目录的文件
![](https://img-blog.csdnimg.cn/2018111418254497.png)
find /westos -type f ---查找/westos下文件类型为一般文件的
3.find -ctime 10 ---查找本地存在天数为10天的文件
![](https://img-blog.csdnimg.cn/20181114182604123.png)
find -ctime -10 ---查找本地存在天数少于10天的文件
![](https://img-blog.csdnimg.cn/20181114182556764.png)
find -ctime +10 ---查找本地存在天数大于10天的文件
![](https://img-blog.csdnimg.cn/2018111418261276.png)
find -cmin +|- 10 ---查找本地存在时间大于,等于,小于10分钟的文件
3)根据文件权限来查找文件
监控窗口:
rm -fr /westos/*
touch /westos/file{1..5}
chmod 000 *
chmod 404 file1
chmod 444 file2
chmod 644 file3
chmod 640 file4
watch -n 1 ls -lR /westos
查看/westos/下文件的权限,方便实验
实验窗口:
1.find /westos/ -perm -444 ---查找/westos/下所有权限包含444的文件(与的关系,三个条件,u包含4,g包含u,o包含u)
![](https://img-blog.csdnimg.cn/20181114183851545.png)
find /westos/ -perm -004 ---一个条件,ug忽略,o包含4的文件
![](https://img-blog.csdnimg.cn/20181114183856562.png)
find /westos/ -perm -644 ---4个条件
![](https://img-blog.csdnimg.cn/20181114183903798.png)
find /westos/ -perm -777 ---九个条件
![](https://img-blog.csdnimg.cn/20181114183910613.png)
2.find /westos/ -perm /444 ---查找权限包含444的文件(或的关系,u包含4或者g包含4或者o包含4)
![](https://img-blog.csdnimg.cn/20181114183918775.png)
find /westos/ -perm /777 ---查找但凡包含任意一个权限的文件
#find命令应用