find指令用于在系统中查找指定的文件,查找需要的时间比较长,因为它是从硬盘查找。

 

find  路径  选项 动作

1.根据时间查找

 

-atime :被访问过的时间

-ctime :文件权限改变的时间

-mtime :被修改的时间(比较常用,以此为例)

 

-mtime +n :文件更改时间在n天之前(不含n天)

-mtime -n :文件更改时间在n天之内(含那天)

-mtime n  :文件修改时间在n天前的一天内。

 

1.查找过去系统上24小时内被修改的文件

 

[root@zhu1 ~]# find / -mtime 0

 

2.查找在5日以内被修改的文件

 

[root@zhu1 ~]# find / -mtime -m -5

3.查找在/var/adm 目录下3天前修改的文件

[root@zhu1 ~]# find /var/adm  -mtime +3

 

2.根据比较文件的新旧查找

 

-newer filename1 :查找比文件filename1新的文件

 

-newer filename1  !filename1 :比文件filename1新比filename2旧的文件

 

[root@zhu1 ~]# find /root/ -newer /root/txt1
/root/
/root/txt2
[root@zhu1 ~]# find /root/ -type f -newer /root/txt1
/root/txt2

3.根据文件的类型查找

-type  

 

b :块设备文件

d :目录

c :字符设备文件

f :普通文件

l :链接文件

 

 

查找/root/zhu目录下的普通文件

 

[root@zhu1 ~]# find /root/zhu/ -type f
/root/zhu/txt2
/root/zhu/txt1

 

[root@zhu1 ~]# find /root/zhu/ -type d
/root/zhu/
/root/zhu/jiang

4.根据用户和用户组查找

 

-uid n :n为用户的uid

-gid n :n为用户组的gid

-user name :name为用户的账号名称

-group name :用户组名

-nouser :文件的所有者不存在

-nogroup :文件的所属组不存在

 

[root@zhu1 ~]# find /root -user mysql
/root/zhu
/root/zhu/txt2
/root/zhu/txt1
/root/zhu/jiang
[root@zhu1 ~]# find /root -type f -user mysql
/root/zhu/txt2
/root/zhu/txt1
[root@zhu1 ~]# find /root -group  mysql
/root/zhu
/root/zhu/txt2
/root/zhu/txt1
/root/zhu/jiang

查找不存在所属主的文件

 

[root@zhu1 ~]# find / -nouser

5.根据文件的权限与大小查找

-perm 755 :查找文件权限等于755的文件

-perm -755 :查找文件权限包含755的文件

-perm  +755 :查找文件权限包含755部分权限的文件

 

6.find查询后可进行的操作

 

-print :把查找的内容输出到屏幕上(默认选项)

-exec 'command' {} \;

 

 

 

在/logs目录下查找更改时间在5天以前的文件并删除

find /logs  -type f -mtime +5 -exec rm {} \;

[root@zhu1 ~]# find /root -type f -user mysql
/root/zhu/txt2
/root/zhu/txt1
[root@zhu1 ~]# find /root -type f -user mysql -exec rm {} \;
[root@zhu1 ~]# find /root -type f -user mysql

find 与xargs结合使用