find用途

1
2
3
4
5
6
7
8
1.忘了某个文件所在的位置,可以通过find来查找。
2.可以查找某个目录下,所有小于1k的文件。
3.可以查找某个目录下,7天之前创建的文件。
4.可以查找某目录下所有以.sh结尾的脚本。

它可以根据不同的条件来进行查找文件:例如权限、拥有者、修改日期/时间、文件大小等等。 同时find命令是Linux下必须掌握的。

[root@lb01 code]# find /code/ -size -4k ! -type d -delete
命令 路径 选项 表达式 动作
find [path….] [options…] [expression] [action]
逻辑运算符 作用
-a 且 &&
-o 或 \ \
!

find的选项

选项 作用
-name
-iname
根据文件名查找
不区分文件名大小写查找
-type 根据文件类型查找
-size 根据文件大小找
-time 根据文件的时间找
-user
-group
-nouser
-nogroup
查找指定用户
查找指定用户组
查找没有用户的文件查找
没有用户组的文件
-perm 根据文件权限找
-maxdepth 根据句深度查找
-empty 查找空文件(和-type d配合可以指定空目录)

根据文件名查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
-name
## 找到/etc/目录下网卡信息配置文件在哪
[root@localhost ~]# find /etc -name '*ens33*' 加上*是模糊查找

## 在/etc/目录下找以.conf结尾的所有配置文件
[root@localhost ~]# find /etc -name '*.conf'

## 在/etc/目录下找到以.conf结尾的文件和.sh结尾的文件
[root@localhost ~]# find /etc/ -name '*.conf' && find /etc/ -name '*.sh'
[root@localhost ~]# find /etc/ -name '*.conf';find /etc/ -name '*.sh'
[root@localhost ~]# find /etc/ -name '*.conf' -o -name '*.sh'

## 在/etc/目录下找到不是以.conf结尾的文件
[root@localhost ~]# find /etc/ ! -name '*.conf'

## 精确匹配
[root@localhost ~]# find /etc/ -name 'ifcfg-ens33'

## 不区分大小写查找文件名
[root@localhost ~]# find ./ -iname '*zls*'

根据文件类型查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-type
## 文件类型
普通文件:f
目录: d
软链接文件:l
套接字文件:s
字符设备文件:c
块设备文件:b
管道文件:p
//f 文件
[root@zls ~]# find /dev -type f
//d 目录
[root@zls ~]# find /dev -type d
//l 链接
[root@zls ~]# find /dev -type l
//b 块设备
[root@zls ~]# find /dev -type b
//c 字符设备
[root@zls ~]# find /dev -type c
//s 套接字
[root@zls ~]# find /dev -type s
//p 管道文件
[root@zls ~]# find /dev -type p

## 查找/etc/目录下,文件名中包含conf的目录
[root@localhost ~]# find /etc -name '*conf*' -type d
[root@localhost ~]# find /etc -name '*conf*' -type d -ls

根据文件大小查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
-size
+:大于
-:小于
## 找小于多少的文件
[root@localhost ~]# find /tmp -size -4k
## 找大于多少的文件
[root@localhost ~]# find /tmp -size +4k
## 找等于多少
[root@localhost ~]# find /tmp -size 4k

## 找到/code目录下小于4k的文件并删除
[root@localhost ~]# find /code -size -4k -delete

## 找到/code目录下小于4k的文件并删除(不删除目录)
[root@localhost ~]# find /code -size -4k ! -type d -delete
##! -type d -delete非目录

根据文件的时间查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
## 一个文件的时间
[root@localhost ~]# stat 1.txt
File: ‘1.txt’
Size: 4 Blocks: 8 IO Block: 4096 regular file
Device: 803h/2051d Inode: 16797763 Links: 1
Access: (0644/-rw-r--r--) Uid: ( 0/ root) Gid: ( 0/ root)
Context: unconfined_u:object_r:admin_home_t:s0
Access: 2024-04-17 19:25:18.951724354 +0800
Modify: 2024-04-17 19:25:18.951724354 +0800
Change: 2024-04-17 19:25:18.951724354 +0800

-mtime:Modify 修改时间
-atime:Access 访问时间
-ctime:Change 时间戳修改时间

+:查找N天之前的(不包含今天)
-:查找N天之内的(包含今天)
N:查找往前数第N天的(不包含今天) (-mtime后只有数字的)

## 不加任何符号:查找不算今天往前数第7天的文件
-mtime 7
[root@localhost find]# find /root/find/ -mtime 7

## +7 不算今天往前数第七天之前的文件
[root@localhost find]# find /root/find/ -mtime +7
## -7 算今天,最近7天的文件
[root@localhost find]# find /root/find/ -mtime -7
## 需求:保留一周的数据
[root@localhost find]# find /root/find/ ! -mtime -7 -delete

根据用户查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-user:查找指定用户
-group:查找指定用户组
-nouser:查找没有用户的文件
-nogroup:查找没有用户组的文件
## 查找属于root用户adm组的文件
[root@localhost ~]# find /root/ -user root -group adm -ls
## 查找user是zls,或group是adm组的文件
[root@localhost ~]# find /root/ \( -group adm -o -user zls \) -ls
16797763 4 -rw-r--r-- 1 zls root 4 Apr 17 2024 /root/1.txt
16797778 4 -rw-r--r-- 1 zls root 4 Apr 16 2024 /root/2.txt
16797816 4 -rw-r--r-- 1 root adm 4 Apr 17 2024
/root/zls.txt
16851189 4 -rw-r--r-- 1 root adm 4 Apr 16 2024
/root/zls1.txt
16851191 4 -rw-r--r-- 1 root adm 4 Apr 16 2024
/root/zls2.txt
16797780 4 -rw-r--r-- 1 root adm 4 Apr 16 2024
/root/zls3.txt
16797813 4 -rw-r--r-- 1 root adm 4 Apr 16 2024
/root/zls4.txt
[root@localhost ~]# find /root/ \( -user zls -o -group adm \)
-rw-r--r--. 1 zls root 4 Apr 17 2024 /root/1.txt
-rw-r--r--. 1 zls root 4 Apr 16 2024 /root/2.txt
-rw-r--r--. 1 root adm 4 Apr 16 2024 /root/zls1.txt
-rw-r--r--. 1 root adm 4 Apr 16 2024 /root/zls2.txt
-rw-r--r--. 1 root adm 4 Apr 16 2024 /root/zls3.txt
-rw-r--r--. 1 root adm 4 Apr 16 2024 /root/zls4.txt
-rw-r--r--. 1 root adm 4 Apr 17 2024 /root/zls.txt
## 查找没有用户或者没有组的文件
[root@localhost opt]# find /opt/ -nouser -o -nogroup

根据文件权限查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
perm
## 精确查找文件的权限644
[root@localhost opt]# find /opt/ -perm 644 -ls
16851186 0 -rw-r--r-- 1 1000 oldboyedu 0 Apr 15 2024
/opt/sbit/1.txt
16851188 0 -rw-r--r-- 1 oldboy oldboyedu 0 Apr 15 2024
/opt/sbit/2.txt
8393463 0 -rw-r--r-- 1 1002 root 0 Mar 30 00:19 /opt/1.txt
8393464 0 -rw-r--r-- 1 root 1000 0 Mar 30 00:19 /opt/2.txt
## -:代表该权限位上,必须包含该数字权限622 rw w w
[root@localhost opt]# find /opt/ -perm -622 -ls
16851181 0 drwxrwxrwt 2 root root 32 Apr 15 2024 /opt/sbit
## /:拥有者至少有r或w权限, 或者拥有组至少有w权限, 或者匿名至少有w权限
[root@zls ~]# find /home -perm /622
8405111 0 drwxr-xr-x 4 root root 77 Mar 30 00:19 /opt/
16777 0 drwxr-sr-x 2 1000 root 19 Apr 15 2024 /opt/test
16779 0 -rw-rw-r-- 1 1000 root 0 Apr 15 2024
/opt/test/1.txt
16851181 0 drwxrwxrwt 2 root root 32 Apr 15 2024 /opt/sbit
16851186 0 -rw-r--r-- 1 1000 oldboyedu 0 Apr 15 2024
/opt/sbit/1.txt
16851188 0 -rw-r--r-- 1 oldboy oldboyedu 0 Apr 15 2024
/opt/sbit/2.txt
8393448 12 -rw------- 1 root root 9567 Apr 16 2024
/opt/.bash_history
8393463 0 -rw-r--r-- 1 1002 root 0 Mar 30 00:19 /opt/1.txt

根据深度查找

1
[root@localhost ~]# find /etc/ -maxdepth 1 -type d

动作

动作 作用
-print 打印结果(find 默认就打印结果)
-ls 查看文件详细信息(如果有逻辑运算符,就只查看逻辑运算符后面文件详细信息,除非 使用小括号整体括起来)
- delete 删除查找到的文件(如果有逻辑运算符,就只查看逻辑运算符后面文件详细信息,除非 使用小括号整体括起来)
-ok 执行后面的shell命令(会询问)(如果有逻辑运算符,就只查看逻辑运算符后面文件详 细信息,除非使用小括号整体括起来)
-exec 执行后面的shell命令(不询问)(如果有逻辑运算符,就只查看逻辑运算符后面文件详 细信息,除非使用小括号整体括起来)
1
2
3
4
5
6
7
[root@localhost ~]# find /opt/ -type f -o -name '*st' -exec rm -fr {} \;
[root@localhost ~]# find /opt/ -type f -o -name '*st' -ok rm -fr {} \;
## 以后动作都改为xargs 后面加命令
[root@localhost ~]# find /opt/ -type f -o -name '*st'|xargs rm -fr
## 拷贝
[root@localhost ~]# find /root/ -type f|xargs cp -t /tmp/
[root@localhost ~]# find /root/ -type d|xargs -I {} cp -r {} /opt/