系统管理 - Linux系统文件查找

1. 文件查找概述

Linux系统中的find命令在查找文件时非常有用而且方便。

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

1.1 find命令基本语法

1
find [搜索路径] [搜索条件] [处理动作]

基本格式:

  • 搜索路径: 指定查找的目录,可以是多个目录
  • 搜索条件: 指定查找的条件(名称、大小、时间、用户等)
  • 处理动作: 对找到的文件执行的操作(默认是-print)

1.2 find命令特点

  • 功能强大: 支持多种查找条件
  • 递归查找: 默认递归查找子目录
  • 精确匹配: 支持精确匹配和模糊匹配
  • 组合条件: 支持多个条件组合(AND、OR)
  • 执行动作: 可以对找到的文件执行各种操作

2. find名称查找

2.1 基本名称查找

1
2
3
4
5
6
7
8
# 创建测试文件
touch /etc/sysconfig/network-scripts/{ifcfg-eth1,IFCFG-ETH1}

# 查找/etc目录下包含ifcfg-eth1名称的文件(精确匹配)
[root@liyanzhao ~]# find /etc -name "ifcfg-eth1"

# -i 忽略大小写
[root@liyanzhao ~]# find /etc -iname "ifcfg-eth1"

2.2 通配符查找

1
2
3
4
5
# 查找/etc目录下包含ifcfg-eth名称所有文件
[root@liyanzhao ~]# find /etc/ -name "ifcfg-eth*"

# 忽略大小写
[root@liyanzhao ~]# find /etc -iname "ifcfg-eth*"

2.3 名称查找常用模式

模式 说明 示例
"filename" 精确匹配 find /etc -name "passwd"
"file*" 以file开头 find /etc -name "ifcfg*"
"*file" 以file结尾 find /etc -name "*.conf"
"*file*" 包含file find /etc -name "*network*"
"file?" 单个字符匹配 find /etc -name "file?"

2.4 名称查找选项

选项 功能 示例
-name 区分大小写匹配 find /etc -name "*.conf"
-iname 不区分大小写匹配 find /etc -iname "*.conf"

3. find大小查找

3.1 大小查找语法

1
find [路径] -size [+|-|=]大小

大小单位:

  • c: 字节(默认)
  • k: KB
  • M: MB
  • G: GB

3.2 大小查找示例

1
2
3
4
5
6
7
8
# 查找大于5M的文件
[root@liyanzhao ~]# find /etc -size +5M

# 查找等于5M的文件
[root@liyanzhao ~]# find /etc -size 5M

# 查找小于5M的文件
[root@liyanzhao ~]# find /etc -size -5M

3.3 大小查找说明

符号 说明 示例
+ 大于指定大小 find /etc -size +5M
- 小于指定大小 find /etc -size -5M
无符号 等于指定大小(近似) find /etc -size 5M

3.4 大小查找实战

1
2
3
4
5
6
7
8
9
10
11
# 查找大于100MB的文件
find /var -size +100M

# 查找小于1KB的文件
find /etc -size -1k

# 查找空文件
find /tmp -size 0

# 查找大文件(用于清理)
find /var/log -size +500M -ls

4. find时间查找

4.1 时间查找类型

选项 说明 时间类型
-mtime 修改时间(内容修改)
-ctime 改变时间(属性改变)
-atime 访问时间
-mmin 修改时间 分钟
-cmin 改变时间 分钟
-amin 访问时间 分钟

4.2 时间查找语法

1
find [路径] -mtime [+|-|=]天数

时间说明:

  • +n: n天以前(不包含第n天)
  • -n: n天以内(包含第n天)
  • n: 正好第n天(不包含当天)

4.3 时间查找示例

1
2
3
4
5
6
7
8
9
10
11
# 创建测试文件
[root@liyanzhao ~]# for i in {01..28};do date -s 201802$i && touch file-$i;done

# 查找7天以前的文件(不会打印当天的文件)
[root@liyanzhao ~]# find ./ -iname "file-*" -mtime +7

# 查找最近7天的文件,不建议使用(会打印当天的文件)
[root@liyanzhao ~]# find ./ -iname "file-*" -mtime -7

# 查找第7天文件(不会打印当天的文件)
[root@liyanzhao ~]# find ./ -iname "file-*" -mtime 7

4.4 时间查找实战案例

1
2
3
4
5
6
7
8
9
10
11
12
# 本地文件保留最近7天的备份文件, 备份服务器保留3个月的备份文件
find /backup/ -iname "*.bak" -mtime +7 -delete
find /backup/ -iname "*.bak" -mtime +90 -delete

# 查找最近24小时修改的文件
find /var/log -mtime -1

# 查找最近30分钟修改的文件
find /tmp -mmin -30

# 查找7天前访问的文件(可用于清理)
find /var/cache -atime +7 -delete

4.5 时间查找详细说明

表达式 含义 说明
-mtime +7 7天以前 不包含第7天和当天
-mtime -7 7天以内 包含第7天和当天
-mtime 7 正好第7天 不包含当天
-mmin +30 30分钟以前 不包含第30分钟
-mmin -30 30分钟以内 包含第30分钟

5. find用户查找

5.1 用户查找选项

选项 功能 示例
-user 查找指定用户的文件 find /home -user jack
-group 查找指定组的文件 find /home -group admin
-nouser 查找没有属主的文件 find /home -nouser
-nogroup 查找没有属组的文件 find /home -nogroup

5.2 用户查找示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 查找属主是jack
[root@liyanzhao ~]# find /home -user jack

# 查找属组是admin
[root@liyanzhao ~]# find /home -group admin

# 查找属主是jack, 属组是admin(AND关系)
[root@liyanzhao ~]# find /home -user jack -group admin

# 查找属主是jack, 并且属组是admin(显式AND)
[root@liyanzhao ~]# find /home -user jack -a -group admin

# 查找属主是jack, 或者属组是admin(OR关系)
[root@liyanzhao ~]# find /home -user jack -o -group admin

# 查找没有属主
[root@liyanzhao ~]# find /home -nouser

# 查找没有属组
[root@liyanzhao ~]# find /home -nogroup

# 查找没有属主或属组(OR关系)
[root@liyanzhao ~]# find /home -nouser -o -nogroup

5.3 逻辑运算符

运算符 说明 示例
-a 或 空格 AND(与) find /home -user jack -a -group admin
-o OR(或) find /home -user jack -o -group admin
!-not NOT(非) find /home ! -user root

5.4 用户查找实战

1
2
3
4
5
6
7
8
9
10
11
# 查找root用户的所有文件
find /home -user root

# 查找不属于任何用户的文件(可能的安全问题)
find / -nouser -ls

# 查找不属于任何组的文件
find / -nogroup -ls

# 查找特定用户和组的文件
find /var/www -user www-data -group www-data

6. find类型查找

6.1 文件类型选项

选项 类型 说明 示例
-type f 普通文件 常规文件 find /dev -type f
-type d 目录 目录文件 find /dev -type d
-type l 链接文件 符号链接 find /dev -type l
-type b 块设备 块设备文件 find /dev -type b
-type c 字符设备 字符设备文件 find /dev -type c
-type s 套接字 Socket文件 find /dev -type s
-type p 管道文件 命名管道 find /dev -type p

6.2 类型查找示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# f 文件
[root@liyanzhao ~]# find /dev -type f

# d 目录
[root@liyanzhao ~]# find /dev -type d

# l 链接
[root@liyanzhao ~]# find /dev -type l

# b 块设备
[root@liyanzhao ~]# find /dev -type b

# c 字符设备
[root@liyanzhao ~]# find /dev -type c

# s 套接字
[root@liyanzhao ~]# find /dev -type s

# p 管道文件
[root@liyanzhao ~]# find /dev -type p

6.3 类型查找实战

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 查找所有普通文件
find /tmp -type f

# 查找所有目录
find /var -type d

# 查找所有符号链接
find /usr/bin -type l

# 查找块设备
find /dev -type b -ls

# 查找字符设备
find /dev -type c -ls

7. find权限查找

7.1 权限查找语法

1
find [路径] -perm [模式]

权限模式:

  • 精确匹配: -perm 644 - 权限必须完全匹配
  • 至少匹配: -perm -644 - 权限至少包含指定权限
  • 任意匹配: -perm /644 - 任意位匹配即可

7.2 权限查找示例

1
2
3
4
5
6
7
8
9
10
11
# 精确匹配644权限
[root@liyanzhao ~]# find . -perm 644 -ls

# 拥有者至少有011(-wx),组010(-w-),其他人100(r--)
[root@liyanzhao ~]# find /home -perm -324

# 查找全局可写(每位权限必须高于2 -w-)
[root@liyanzhao ~]# find . -perm -222 -ls

# 拥有者至少有r权限, 或者拥有组至少有r权限, 或者匿名至少有w权限
[root@liyanzhao ~]# find /home -perm /442

7.3 权限查找说明

模式 说明 示例
-perm 644 精确匹配 权限必须正好是644
-perm -644 至少匹配 权限至少包含644(644、755等)
-perm /644 任意匹配 任意位匹配即可

7.4 特殊权限查找

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 包含set uid
[root@liyanzhao ~]# find /usr/sbin -perm -4000 -ls

# 包含set gid
[root@liyanzhao ~]# find /usr/sbin -perm -2000 -ls

# 包含sticky
[root@liyanzhao ~]# find /usr/sbin -perm -1000 -ls

# 查找所有SUID文件
find /usr -perm -4000 -type f -ls

# 查找所有SGID文件
find /usr -perm -2000 -type f -ls

# 查找所有Sticky位文件
find /tmp -perm -1000 -type d -ls

7.5 权限查找实战

1
2
3
4
5
6
7
8
9
10
11
# 查找全局可写文件(安全风险)
find /var -perm -222 -type f -ls

# 查找全局可执行文件
find /tmp -perm -111 -type f -ls

# 查找SUID和SGID文件(安全审计)
find / -perm -6000 -type f -ls

# 查找权限过宽的文件
find /home -perm -077 -type f -ls

8. find处理动作

当查找到一个文件后, 需要对文件进行如何处理, 默认动作是 -print

8.1 处理动作选项

选项 功能 说明
-print 打印(默认) 打印找到的文件路径
-ls 以长格式打印显示 类似ls -l的输出
-delete 删除查找到的文件 仅能删除空目录
-exec 执行自定义命令 标准写法 -exec {} \;
-ok 执行自定义命令(交互) 会提示是否操作

8.2 打印动作

1
2
3
4
5
6
7
8
# 打印查询到的文件(默认动作)
[root@liyanzhao ~]# find /etc -name "ifcfg*"

# 显式指定打印
[root@liyanzhao ~]# find /etc -name "ifcfg*" -print

# 以长格式打印显示
[root@liyanzhao ~]# find /etc -name "ifcfg*" -ls

8.3 删除动作

1
2
3
4
5
6
7
8
9
10
11
# 使用-exec删除文件
[root@liyanzhao ~]# find /etc -name "ifcfg*" -exec rm -f {} \;

# 使用-delete删除文件(更简洁)
[root@liyanzhao ~]# find /etc -name "ifcfg*" -delete

# 删除空目录
find /tmp -type d -empty -delete

# 删除7天前的日志文件
find /var/log -name "*.log" -mtime +7 -delete

8.4 执行动作(-exec)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 拷贝文件
[root@liyanzhao ~]# find /etc -name "ifcfg*" -exec cp -rvf {} /tmp \;

# 移动文件
find /tmp -name "*.tmp" -exec mv {} /backup/ \;

# 修改文件权限
find /var/www -type f -exec chmod 644 {} \;

# 修改目录权限
find /var/www -type d -exec chmod 755 {} \;

# 查找并压缩文件
find /var/log -name "*.log" -exec gzip {} \;

8.5 交互执行动作(-ok)

1
2
3
4
5
6
7
# -ok会不断提示是否执行
[root@liyanzhao ~]# find /etc -name "ifcfg*" -ok cp -rvf {} /tmp \;
< cp ... /etc/sysconfig/network-scripts/ifcfg-eth0 /tmp > ? y
< cp ... /etc/sysconfig/network-scripts/ifcfg-eth1 /tmp > ? n

# 交互删除文件
find /tmp -name "*.tmp" -ok rm {} \;

8.6 -exec语法说明

1
2
3
4
5
6
7
8
9
10
# 标准语法
find [路径] [条件] -exec command {} \;

# {} 表示找到的文件
# \; 表示命令结束

# 示例
find /etc -name "*.conf" -exec ls -l {} \;

# 注意:{} 和 \; 之间有空格

9. find结合xargs

9.1 find与xargs结合

xargs可以将find查找到的结果一个一个的处理,比-exec更高效。

1
2
3
4
5
6
# xargs将查找到结果一个一个的处理
[root@liyanzhao ~]# touch file.txt
[root@liyanzhao ~]# find . -name "file.txt" |xargs rm -f

# 使用-I选项指定占位符
[root@liyanzhao~]# find . -name "file.txt" |xargs -I {} cp -rvf {} /var/tmp

9.2 find与xargs对比

方式 语法 特点
-exec find ... -exec cmd {} \; 每个文件执行一次命令
xargs find ... | xargs cmd 批量处理,更高效

9.3 find与xargs示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 删除文件
find /tmp -name "*.tmp" |xargs rm -f

# 复制文件
find /etc -name "*.conf" |xargs -I {} cp {} /backup/

# 修改权限
find /var/www -type f |xargs chmod 644

# 查找并grep
find /etc -name "*.conf" |xargs grep "root"

# 统计文件行数
find /etc -name "*.conf" |xargs wc -l

# 压缩文件
find /var/log -name "*.log" |xargs gzip

9.4 xargs常用选项

选项 功能 示例
-I {} 指定占位符 find ... | xargs -I {} cp {} /tmp
-n 每次传递的参数个数 find ... | xargs -n 2
-p 交互式确认 find ... | xargs -p rm
-t 显示执行的命令 find ... | xargs -t rm

10. find组合条件

10.1 逻辑运算符

1
2
3
4
5
6
7
8
9
10
11
12
# AND关系(默认,多个条件用空格连接)
find /home -user jack -group admin

# 显式AND
find /home -user jack -a -group admin

# OR关系
find /home -user jack -o -group admin

# NOT关系
find /home ! -user root
find /home -not -user root

10.2 组合条件示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 查找jack用户的所有.conf文件
find /home -user jack -name "*.conf"

# 查找大于100M且小于1G的文件
find /var -size +100M -size -1G

# 查找7天前修改的.log文件
find /var/log -name "*.log" -mtime +7

# 查找root用户的可执行文件
find /usr/bin -user root -type f -perm -111

# 查找空文件或空目录
find /tmp \( -type f -empty \) -o \( -type d -empty \)

10.3 优先级控制

1
2
3
4
5
6
# 使用括号控制优先级(需要转义)
find /home \( -user jack -o -user alice \) -name "*.txt"

# 查找jack或alice用户的.txt文件
find /home -user jack -o -user alice -name "*.txt" # 错误
find /home \( -user jack -o -user alice \) -name "*.txt" # 正确

11. find实战案例

11.1 案例1: 清理临时文件

1
2
3
4
5
6
7
8
# 清理7天前的临时文件
find /tmp -type f -mtime +7 -delete

# 清理空文件
find /tmp -type f -empty -delete

# 清理特定扩展名的文件
find /tmp -name "*.tmp" -mtime +3 -delete

11.2 案例2: 查找大文件

1
2
3
4
5
6
7
8
# 查找大于100M的文件
find /var -type f -size +100M -ls

# 查找最大的10个文件
find /var -type f -exec ls -lh {} \; |sort -k5 -hr |head -10

# 查找并显示文件大小
find /var/log -type f -size +50M -exec ls -lh {} \;

11.3 案例3: 安全审计

1
2
3
4
5
6
7
8
9
10
11
# 查找SUID文件
find /usr -perm -4000 -type f -ls

# 查找全局可写文件
find /var -perm -002 -type f -ls

# 查找没有属主的文件
find / -nouser -ls

# 查找没有属组的文件
find / -nogroup -ls

11.4 案例4: 日志管理

1
2
3
4
5
6
7
8
# 查找并压缩7天前的日志
find /var/log -name "*.log" -mtime +7 -exec gzip {} \;

# 查找并删除30天前的日志
find /var/log -name "*.log" -mtime +30 -delete

# 查找并备份日志
find /var/log -name "*.log" -mtime +7 |xargs -I {} cp {} /backup/

11.5 案例5: 文件备份

1
2
3
4
5
6
7
8
# 查找配置文件并备份
find /etc -name "*.conf" -exec cp {} /backup/etc/ \;

# 查找并打包
find /var/www -name "*.php" |xargs tar czf php_backup.tar.gz

# 查找并同步
find /data -type f -mtime -1 |xargs -I {} rsync -av {} /backup/

12. find性能优化

12.1 优化建议

  1. 限制搜索范围: 尽量指定具体的搜索路径
  2. 使用索引: 对于经常搜索的目录,考虑使用locate
  3. 避免深度搜索: 使用-maxdepth限制搜索深度
  4. 组合条件顺序: 将选择性高的条件放在前面

12.2 限制搜索深度

1
2
3
4
5
6
7
8
# 只搜索当前目录(不递归)
find . -maxdepth 1 -name "*.txt"

# 只搜索2层目录
find /var -maxdepth 2 -name "*.log"

# 限制最小深度
find /var -mindepth 2 -maxdepth 4 -name "*.conf"

12.3 性能对比

1
2
3
4
5
6
7
8
# 慢:先查找所有文件,再过滤
find / -name "*.conf"

# 快:限制搜索范围
find /etc -name "*.conf"

# 更快:限制深度
find /etc -maxdepth 2 -name "*.conf"

13. 命令总结

13.1 find查找条件

条件类型 选项 示例
名称 -name, -iname find /etc -name "*.conf"
大小 -size find /var -size +100M
时间 -mtime, -mmin find /tmp -mtime +7
用户 -user, -group find /home -user jack
类型 -type find /dev -type f
权限 -perm find /usr -perm -4000

13.2 find处理动作

动作 选项 示例
打印 -print, -ls find /etc -name "*.conf" -ls
删除 -delete find /tmp -name "*.tmp" -delete
执行 -exec find /etc -name "*.conf" -exec cp {} /tmp \;
交互 -ok find /tmp -name "*.tmp" -ok rm {} \;

13.3 find常用组合

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 查找并删除
find /tmp -name "*.tmp" -mtime +7 -delete

# 查找并复制
find /etc -name "*.conf" -exec cp {} /backup/ \;

# 查找并压缩
find /var/log -name "*.log" -mtime +7 |xargs gzip

# 查找并统计
find /etc -name "*.conf" |wc -l

# 查找并列出详细信息
find /var -size +100M -ls