文件搜索 #

find - 强大的文件搜索 #

基本用法 #

bash
# 在当前目录搜索文件
$ find . -name "file.txt"

# 在指定目录搜索
$ find /home/user -name "file.txt"

# 搜索所有 .txt 文件
$ find . -name "*.txt"

# 忽略大小写
$ find . -iname "*.TXT"

按名称搜索 #

bash
# -name 按文件名搜索(区分大小写)
$ find . -name "*.txt"

# -iname 忽略大小写
$ find . -iname "*.TXT"

# 使用通配符
$ find . -name "file?.txt"
$ find . -name "file[0-9].txt"
$ find . -name "[a-z]*.txt"

# 使用正则表达式
$ find . -regex ".*\.[0-9]+\.txt"
$ find . -iregex ".*\.TXT$"

# 排除特定文件
$ find . -name "*.txt" ! -name "temp*"

按类型搜索 #

bash
# -type 按文件类型搜索
$ find . -type f        # 普通文件
$ find . -type d        # 目录
$ find . -type l        # 符号链接
$ find . -type b        # 块设备
$ find . -type c        # 字符设备
$ find . -type p        # 命名管道
$ find . -type s        # 套接字

# 组合使用
$ find . -type f -name "*.txt"
$ find . -type d -name "test*"

按时间搜索 #

bash
# -mtime 按修改时间(天)
$ find . -mtime 7       # 正好 7 天前修改
$ find . -mtime +7      # 超过 7 天前修改
$ find . -mtime -7      # 7 天内修改

# -atime 按访问时间(天)
$ find . -atime -1      # 1 天内访问过

# -ctime 按状态改变时间(天)
$ find . -ctime -1      # 1 天内状态改变

# -mmin 按修改时间(分钟)
$ find . -mmin -60      # 60 分钟内修改
$ find . -mmin +30      # 超过 30 分钟前修改

# -newer 比某个文件新
$ find . -newer reference.txt

# 组合使用
$ find . -mtime +30 -mtime -60    # 30-60 天前修改

按大小搜索 #

bash
# -size 按文件大小
$ find . -size 100M     # 正好 100M
$ find . -size +100M    # 大于 100M
$ find . -size -100M    # 小于 100M

# 大小单位
$ find . -size +1k      # 大于 1KB
$ find . -size +1M      # 大于 1MB
$ find . -size +1G      # 大于 1GB
$ find . -size +1c      # 大于 1 字节

# 查找大文件
$ find . -type f -size +100M

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

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

按权限搜索 #

bash
# -perm 按权限搜索
$ find . -perm 755      # 权限正好是 755
$ find . -perm -755     # 权限至少包含 755
$ find . -perm /755     # 权限包含任意一位

# 查找可执行文件
$ find . -perm -111
$ find . -perm /111

# 查找可写文件
$ find . -perm -222
$ find . -perm /u+w

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

# 查找 SGID 文件
$ find / -perm -2000

按所有者搜索 #

bash
# -user 按所有者
$ find . -user root

# -group 按组
$ find . -group developers

# -uid 按用户 ID
$ find . -uid 1000

# -gid 按组 ID
$ find . -gid 1000

# -nouser 没有所有者
$ find . -nouser

# -nogroup 没有组
$ find . -nogroup

组合条件 #

bash
# AND 条件(默认)
$ find . -name "*.txt" -type f

# OR 条件 (-o)
$ find . -name "*.txt" -o -name "*.md"

# NOT 条件 (! 或 -not)
$ find . ! -name "*.txt"
$ find . -not -name "*.txt"

# 复杂组合
$ find . \( -name "*.txt" -o -name "*.md" \) -type f
$ find . -type f \( -name "*.txt" -a -size +1M \)
$ find . -type f ! \( -name "*.txt" -o -name "*.md" \)

执行操作 #

bash
# -print 打印结果(默认)
$ find . -name "*.txt" -print

# -ls 显示详细信息
$ find . -name "*.txt" -ls

# -delete 删除文件
$ find . -name "*.tmp" -delete

# -exec 执行命令
$ find . -name "*.txt" -exec cat {} \;
$ find . -name "*.txt" -exec rm {} \;
$ find . -type d -exec chmod 755 {} \;

# -exec 批量执行(更高效)
$ find . -name "*.txt" -exec cat {} +
$ find . -name "*.txt" -exec rm {} +

# -ok 执行前确认
$ find . -name "*.tmp" -ok rm {} \;

# 使用 xargs
$ find . -name "*.txt" | xargs cat
$ find . -name "*.txt" -print0 | xargs -0 cat

实用示例 #

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

# 查找大文件并排序
$ find . -type f -size +100M -exec ls -lh {} \; | sort -k5 -h

# 查找并修改权限
$ find . -type f -exec chmod 644 {} \;
$ find . -type d -exec chmod 755 {} \;

# 查找并打包
$ find . -name "*.txt" | tar -czf texts.tar.gz -T -

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

# 查找重复文件
$ find . -type f -exec md5sum {} \; | sort | uniq -w32 -dD

# 查找最近修改的文件
$ find . -type f -mmin -60 -ls

# 统计文件数量
$ find . -type f | wc -l
$ find . -name "*.py" | wc -l

locate - 快速文件定位 #

基本用法 #

bash
# 搜索文件
$ locate file.txt

# 搜索包含关键字的文件
$ locate nginx

# 忽略大小写
$ locate -i FILE.txt

# 限制结果数量
$ locate -n 10 nginx

# 显示统计信息
$ locate -S
Database /var/lib/mlocate/mlocate.db:
    123,456 directories
    789,012 files
    1,234,567 bytes in file names
    567,890 bytes used to store database

数据库更新 #

bash
# 手动更新数据库
$ sudo updatedb

# 更新后搜索
$ updatedb && locate newfile.txt

# 查看数据库配置
$ cat /etc/updatedb.conf

locate vs find #

text
┌─────────────────────────────────────────────────────────────┐
│                    locate vs find                           │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   locate                                                    │
│   ├── 使用数据库,速度极快                                   │
│   ├── 可能不是最新结果                                       │
│   ├── 需要定期更新数据库                                     │
│   └── 适合快速查找已知文件名                                  │
│                                                             │
│   find                                                      │
│   ├── 实时搜索,结果准确                                     │
│   ├── 速度较慢                                               │
│   ├── 支持复杂条件                                           │
│   └── 适合精确搜索和复杂条件                                  │
│                                                             │
└─────────────────────────────────────────────────────────────┘

which - 查找命令位置 #

基本用法 #

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

$ which python
/usr/bin/python

# 查找多个命令
$ which ls cp mv
/bin/ls
/bin/cp
/bin/mv

# 显示所有匹配
$ which -a python
/usr/bin/python
/usr/local/bin/python

# 查找别名
$ which ll
alias ll='ls -alF'
    /bin/ls

实用场景 #

bash
# 检查命令是否存在
$ which docker
/usr/bin/docker

$ which nonexistent
# 无输出

# 查找脚本位置
$ which myscript.sh

# 在脚本中使用
if command -v git &> /dev/null; then
    echo "Git is installed at $(which git)"
fi

whereis - 查找程序文件 #

基本用法 #

bash
# 查找程序相关文件
$ whereis ls
ls: /bin/ls /usr/share/man/man1/ls.1.gz

$ whereis python
python: /usr/bin/python /usr/bin/python2.7 /usr/lib/python2.7 /usr/lib/python3.8 /etc/python /usr/local/lib/python

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

# 只查找手册页
$ whereis -m ls
ls: /usr/share/man/man1/ls.1.gz

# 只查找源代码
$ whereis -s ls
ls:

# 查找多个程序
$ whereis ls cp mv

type - 查看命令类型 #

基本用法 #

bash
# 查看命令类型
$ type ls
ls is aliased to `ls --color=auto'

$ type cd
cd is a shell builtin

$ type cat
cat is /bin/cat

$ type ll
ll is aliased to `ls -alF'

# 查看所有定义
$ type -a ls
ls is aliased to `ls --color=auto'
ls is /bin/ls

# 检查是否是内建命令
$ type -t cd
builtin

$ type -t ls
alias

$ type -t cat
file

命令类型 #

text
┌─────────────────────────────────────────────────────────────┐
│                      命令类型                                │
├─────────────────────────────────────────────────────────────┤
│                                                             │
│   alias      别名                                           │
│   builtin    Shell 内建命令                                  │
│   file       外部命令(可执行文件)                           │
│   function   Shell 函数                                     │
│   keyword    Shell 关键字                                   │
│                                                             │
└─────────────────────────────────────────────────────────────┘

grep 搜索文件内容 #

基本用法 #

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

# 在多个文件中搜索
$ grep "pattern" *.txt

# 递归搜索目录
$ grep -r "pattern" directory/

# 忽略大小写
$ grep -i "pattern" file.txt

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

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

# 显示匹配数量
$ grep -c "pattern" file.txt

常用选项 #

bash
# -v 反向匹配
$ grep -v "pattern" file.txt

# -w 匹配整个单词
$ grep -w "word" file.txt

# -A 显示匹配行后的内容
$ grep -A 3 "pattern" file.txt

# -B 显示匹配行前的内容
$ grep -B 3 "pattern" file.txt

# -C 显示匹配行前后的内容
$ grep -C 3 "pattern" file.txt

# -E 使用扩展正则表达式
$ grep -E "pattern1|pattern2" file.txt

# --color 高亮显示
$ grep --color=auto "pattern" file.txt

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

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

正则表达式 #

bash
# 基本正则
$ grep "^start" file.txt      # 以 start 开头
$ grep "end$" file.txt        # 以 end 结尾
$ grep "a.b" file.txt         # a 和 b 之间任意一个字符
$ grep "a*b" file.txt         # a 出现 0 次或多次
$ grep "[abc]" file.txt       # a、b 或 c
$ grep "[^abc]" file.txt      # 不是 a、b、c

# 扩展正则 (-E)
$ grep -E "a+" file.txt       # a 出现 1 次或多次
$ grep -E "a?" file.txt       # a 出现 0 次或 1 次
$ grep -E "a{3}" file.txt     # a 出现 3 次
$ grep -E "a{3,5}" file.txt   # a 出现 3-5 次
$ grep -E "cat|dog" file.txt  # cat 或 dog
$ grep -E "(ab)+" file.txt    # ab 出现 1 次或多次

实用示例 #

bash
# 搜索代码中的函数定义
$ grep -rn "function_name" src/

# 搜索配置项
$ grep -r "DEBUG" /etc/

# 搜索日志中的错误
$ grep -i "error" /var/log/syslog

# 搜索并显示上下文
$ grep -C 5 "exception" error.log

# 统计匹配行数
$ grep -c "ERROR" application.log

# 搜索特定文件类型
$ find . -name "*.py" -exec grep -l "import os" {} \;

# 排除目录
$ grep -r --exclude-dir=node_modules "pattern" .

# 只显示文件名
$ grep -rl "pattern" directory/

小结 #

通过本节学习,你应该掌握:

  1. find:强大的文件搜索,支持各种条件
  2. locate:快速文件定位
  3. which:查找命令位置
  4. whereis:查找程序相关文件
  5. type:查看命令类型
  6. grep:搜索文件内容

下一步,我们将学习文本查看命令。

最后更新:2026-04-11