说道查找命令,第一个想到的就是find,用法多样,加上-exec,你可以玩的很开心。小缺点是需要遍历目录下所有,查找会比较慢。例如遍历/下查找,尤其是系统中文件非常多时,你可以先去听首歌,再来看结果(当然了,这有点夸张!)。

一、语法格式

1、基本例子

[root@salt-master test03]# find /etc/ -name network
/etc/sysconfig/network
/etc/vmware-tools/scripts/vmware/network
/etc/rc.d/init.d/network

一般情况下,查找范围越大,目录层级越深,查找速度就越慢。

[root@salt-master test03]# time find / -name ifcfg-eth0
real 0m0.850s
user 0m0.239s
sys 0m0.598s

注意:real远大于user加上sys,因为find需要遍历各个目录,需要大量的I/O操作,而磁盘I/O通常是最慢的环节,因此大部分时间find进程都在等待磁盘I/O完成。

2、按条件查找

1、按照文件名搜索

-name: 按名字查找

-iname:忽略大小写

[root@salt-master test03]# find /etc -name networkmanager
[root@salt-master test03]# find /etc -iname networkmanager
/etc/NetworkManager

2、按照文件类型查找 -type

f: 普通文件

d: 目录

l: 软连接

......
[root@salt-master test03]# ll
total 4
drwxr-xr-x 2 root root 4096 Dec 13 15:47 01
-rw-r--r-- 1 root root 0 Dec 13 15:47 02
[root@salt-master test03]# find . -type f
./02
[root@salt-master test03]# find . -type d
.
./01

3、按照时间查找

[root@salt-master test03]# stat 文件名 可以查看文件的三个时间

atime:访问时间,cat more less ... ...

mtime:文件的内容发生变化的时间,vim ... ... (也是 ll 所显示的时间)

ctime:文件的属性发生变化的时间,比如:权限改变、大小改变、所有者、所属组等改变

-atime n 以天为单位

-amin n 以分钟为单位

-mtime n 以天为单位

n为数字,前面可以带+或者-号

+n:n+1天之前

n :n到n+1天之间

-n:n天以内

1.查找/find下修改时间在24小时(1天)之内的文件和目录

[root@salt-master test03]# find . -mtime -1
./01
./02

2.查找/find下修改时间在2天前的普通文件

[root@salt-master test03]# ll
total 8
drwxr-xr-x 2 root root 4096 Dec 13 15:47 01
-rw-r--r-- 1 root root 0 Dec 13 15:47 02
-rw-r--r-- 1 root root 193 Apr 12 2019 11.txt
[root@salt-master test03]# find . -type f -mtime +1
./11.txt

4、按照用户和组查找

-user 用户名

-group 组名

-uid uid

-gid gid

-nouser:孤儿文件,没有所有者的文件

-nogroup:没有所属组的文件

1.查找系统中所有者是finduser的文件

[root@salt-master test03]# useradd finduser
[root@salt-master test03]# find / -user finduser -type f
/var/spool/mail/finduser
/home/finduser/.bash_logout
/home/finduser/.bashrc
/home/finduser/.bash_profile

2.查找系统中的孤儿文件

[root@salt-master test03]# userdel finduser
[root@salt-master test03]# find /home/ -type f -nouser
/home/finduser/.bash_logout
/home/finduser/.bashrc
/home/finduser/.bash_profile

6、按照文件大小查找 -size

+ 大于

- 小于

直接数字 等于

‘b’ for 512-byte blocks (this is the default if no suffix is used)
‘c’ for bytes
‘w’ for two-byte words
‘k’ for Kilobytes (units of 1024 bytes)
‘M’ for Megabytes (units of 1048576 bytes)
‘G’ for Gigabytes (units of 1073741824 bytes)
[root@salt-master test03]# ll -h
total 5.1M
drwxr-xr-x 2 root root 4.0K Dec 13 15:47 01
-rw-r--r-- 1 root root 0 Dec 13 15:47 02
-rw-r--r-- 1 root root 5.0M Dec 13 16:40 04
-rw-r--r-- 1 root root 193 Apr 12 2019 11.txt
[root@salt-master test03]# find . -type f -size 3M
[root@salt-master test03]# find . -type f -size -3M

./02

./11.txt
[root@salt-master test03]# find . -type f -size 3M
[root@salt-master test03]# find . -type f -size +3M

./04

3、动作

-exec 动作 -- 找到结果之后直接执行动作

-ok 动作 -- 执行动作之前先提示,即需要交互

[root@salt-master test03]# find . -type f -size +3M
./04
[root@salt-master test03]# find . -type f -size +3M -exec ls -l {} \;
-rw-r--r-- 1 root root 5242880 Dec 13 16:40 ./04

{} —— 用来代替找到的结果

\; —— 表示结束标志

[root@salt-master test03]# find . -type f -size +3M -ok ls -l {} \;
< ls ... ./04 > ? y
-rw-r--r-- 1 root root 5242880 Dec 13 16:40 ./04
[root@salt-master test03]# find . -type f -size +3M -ok ls -l {} \;
< ls ... ./04 > ? n

例如,有时候只想修改当前目录下所有文件权限为644,但不想修改目录权限,可以如下操作

find ./* -type f -exec chmod 644 {} \;

了解了-exec后,find和其他命令的组合起来使用,会使它的功能变得会非常强大和方便。