文件查找

概述

在 Linux 系统中,经常需要查找文件和目录。本章介绍几种常用的文件查找工具:findlocatewhichwhereis 等。

find 命令

find 是最强大的文件查找命令,可以根据各种条件实时搜索文件。

基本语法

find [搜索路径] [条件] [操作]

按名称查找

# 按文件名查找
$ find /home -name "file.txt"

# 不区分大小写
$ find /home -iname "file.txt"

# 使用通配符
$ find /home -name "*.txt"
$ find /home -name "file?.txt"

# 查找特定目录名
$ find /home -name "project*" -type d

按类型查找

# 只查找文件
$ find /home -type f

# 只查找目录
$ find /home -type d

# 只查找符号链接
$ find /home -type l

# 查找空文件
$ find /home -type f -empty

# 查找空目录
$ find /home -type d -empty

文件类型

类型说明
f普通文件
d目录
l符号链接
b块设备
c字符设备
s套接字
p管道

按大小查找

# 大于 100MB 的文件
$ find /home -size +100M

# 小于 1KB 的文件
$ find /home -size -1k

# 正好 100 字节的文件
$ find /home -size 100c

# 介于 10MB 到 100MB 之间
$ find /home -size +10M -size -100M

大小单位

单位说明
c字节
kKB
MMB
GGB

按时间查找

# 最近 7 天内修改的文件
$ find /home -mtime -7

# 7 天前修改的文件
$ find /home -mtime +7

# 正好 7 天前修改的文件
$ find /home -mtime 7

# 最近 60 分钟内修改的文件
$ find /home -mmin -60

# 最近 24 小时内访问的文件
$ find /home -atime -1

# 最近 7 天内状态改变的文件
$ find /home -ctime -7

时间类型

选项说明
-mtime修改时间(内容)
-atime访问时间
-ctime状态改变时间(权限、所有权)
-mmin修改时间(分钟)
-amin访问时间(分钟)
-cmin状态改变时间(分钟)

按权限查找

# 查找权限为 644 的文件
$ find /home -perm 644

# 查找至少有指定权限的文件
$ find /home -perm -644

# 查找具有任一指定权限的文件
$ find /home -perm /644

# 查找 SUID 文件
$ find /usr -perm -4000

# 查找可写文件
$ find /home -perm -222

按所有权查找

# 按用户查找
$ find /home -user maxwell

# 按组查找
$ find /home -group developers

# 查找没有所有者的文件
$ find /home -nouser

# 查找没有所属组的文件
$ find /home -nogroup

组合条件

# AND(默认)
$ find /home -name "*.txt" -size +1M

# OR
$ find /home -name "*.txt" -o -name "*.md"

# NOT
$ find /home ! -name "*.txt"

# 使用括号分组(需要转义)
$ find /home \( -name "*.txt" -o -name "*.md" \) -size +1M

限制搜索深度

# 只在当前目录搜索
$ find /home -maxdepth 1 -name "*.txt"

# 限制最大深度为 2 级
$ find /home -maxdepth 2 -name "*.txt"

# 至少深度为 2 级
$ find /home -mindepth 2 -name "*.txt"

对结果执行操作

-exec 执行命令

# 删除找到的文件
$ find /tmp -name "*.tmp" -exec rm {} \;

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

# 复制文件
$ find /home -name "*.txt" -exec cp {} /backup/ \;

# 显示详细信息
$ find /home -name "*.txt" -exec ls -l {} \;

-exec 使用 + 提高效率

# 一次性处理多个文件(更高效)
$ find /home -name "*.txt" -exec ls -l {} +

# 批量删除
$ find /tmp -name "*.tmp" -exec rm {} +

-ok 交互确认

# 删除前确认
$ find /tmp -name "*.tmp" -ok rm {} \;

使用 xargs

# 基本用法
$ find /home -name "*.txt" | xargs ls -l

# 处理含空格的文件名
$ find /home -name "*.txt" -print0 | xargs -0 ls -l

# 限制每次处理的文件数
$ find /home -name "*.txt" | xargs -n 10 ls -l

实用示例

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

# 查找大文件
$ find / -type f -size +100M 2>/dev/null

# 查找最近修改的配置文件
$ find /etc -name "*.conf" -mtime -1

# 查找并压缩旧文件
$ find /backup -mtime +30 -exec gzip {} \;

# 统计文件数量
$ find /home -type f | wc -l

# 查找重复的文件名
$ find /home -type f -name "*.txt" -printf "%f\n" | sort | uniq -d

locate 命令

locate 使用预建的数据库快速查找文件。

安装

# Debian/Ubuntu
$ sudo apt install mlocate

# Fedora
$ sudo dnf install mlocate

基本使用

# 查找文件
$ locate file.txt

# 不区分大小写
$ locate -i FILE.TXT

# 限制结果数量
$ locate -n 10 "*.txt"

# 只显示存在的文件
$ locate -e file.txt

# 显示匹配数量
$ locate -c "*.txt"

更新数据库

# 手动更新数据库
$ sudo updatedb

find vs locate

特性findlocate
速度较慢(实时搜索)很快(数据库查询)
实时性实时需要更新数据库
功能丰富(条件过滤)简单(名称匹配)
资源消耗较高很低

which 命令

查找命令的可执行文件路径。

# 查找命令位置
$ which ls
/usr/bin/ls

$ which python
/usr/bin/python

# 查找所有匹配
$ which -a python
/usr/bin/python
/usr/local/bin/python

whereis 命令

查找命令的二进制文件、源码和手册页。

$ whereis ls
ls: /usr/bin/ls /usr/share/man/man1/ls.1.gz

$ whereis python
python: /usr/bin/python /usr/lib/python3.10 /usr/share/man/man1/python.1.gz

# 只查找二进制文件
$ whereis -b ls

# 只查找手册页
$ whereis -m ls

type 命令

显示命令的类型和位置。

$ type ls
ls is aliased to 'ls --color=auto'

$ type cd
cd is a shell builtin

$ type python
python is /usr/bin/python

# 显示所有位置
$ type -a ls

grep 搜索文件内容

虽然 grep 主要用于搜索文件内容,但结合其他命令也可用于文件查找。

# 在文件中搜索内容
$ grep "pattern" file.txt

# 递归搜索目录
$ grep -r "pattern" /path/to/dir/

# 只显示文件名
$ grep -l "pattern" *.txt

# 显示行号
$ grep -n "pattern" file.txt

# 不区分大小写
$ grep -i "pattern" file.txt

# 反向匹配(不包含)
$ grep -v "pattern" file.txt

结合 find 使用

# 在 .txt 文件中搜索
$ find /home -name "*.txt" -exec grep -l "keyword" {} \;

# 使用 xargs
$ find /home -name "*.txt" | xargs grep "keyword"

fd - 现代化的 find 替代

fdfind 的现代替代品,更快、更直观。

安装

# Debian/Ubuntu
$ sudo apt install fd-find
# 命令名为 fdfind,可创建别名
$ alias fd=fdfind

# Fedora
$ sudo dnf install fd-find

# Arch Linux
$ sudo pacman -S fd

基本使用

# 简单搜索(默认递归)
$ fd pattern

# 指定目录
$ fd pattern /path/to/dir

# 搜索特定扩展名
$ fd -e txt

# 搜索特定类型
$ fd -t f pattern    # 文件
$ fd -t d pattern    # 目录

# 执行命令
$ fd -e txt -x rm {}

ripgrep (rg) - 快速内容搜索

ripgrep 是最快的内容搜索工具之一。

安装

# Debian/Ubuntu
$ sudo apt install ripgrep

# Fedora
$ sudo dnf install ripgrep

# Arch Linux
$ sudo pacman -S ripgrep

基本使用

# 递归搜索(默认)
$ rg "pattern"

# 在指定目录搜索
$ rg "pattern" /path/to/dir

# 指定文件类型
$ rg -t py "pattern"    # Python 文件
$ rg -t js "pattern"    # JavaScript 文件

# 只显示文件名
$ rg -l "pattern"

# 显示上下文
$ rg -C 3 "pattern"    # 前后 3 行

# 忽略大小写
$ rg -i "pattern"

实用搜索技巧

查找最大的文件

# 前 10 个最大的文件
$ find / -type f -exec du -h {} + 2>/dev/null | sort -rh | head -10

# 或使用 du
$ du -ah /home | sort -rh | head -20

查找最近修改的文件

# 最近 24 小时修改的文件
$ find /home -type f -mtime -1

# 按修改时间排序
$ find /home -type f -printf "%T@ %p\n" | sort -n | tail -10

查找重复文件

# 使用 fdupes
$ sudo apt install fdupes
$ fdupes -r /home

查找损坏的符号链接

$ find /home -xtype l

查找特定大小范围的文件

# 1MB 到 10MB 之间的文件
$ find /home -type f -size +1M -size -10M

排除目录

# 排除 .git 目录
$ find /home -name ".git" -prune -o -name "*.txt" -print

# 排除多个目录
$ find /home \( -name node_modules -o -name .git \) -prune -o -name "*.js" -print

小结

本章介绍了 Linux 中的文件查找工具:

  • find:功能最强大,支持复杂条件搜索
  • locate:快速查找,使用数据库
  • which/whereis/type:查找命令位置
  • grep:搜索文件内容
  • fd:find 的现代替代
  • ripgrep:快速内容搜索

在日常使用中:

  • 简单的文件名搜索用 locate
  • 复杂条件搜索用 find
  • 内容搜索用 grepripgrep
  • 查找命令用 which

掌握这些工具,你将能够在庞大的文件系统中快速找到所需的文件。


上一章:文件权限

下一章:Shell 简介