# Linux 文件检索命令
## 目录
– [find 命令](#一find-命令)
– [grep 命令](#二grep-命令)
– [locate 命令](#三locate-命令)
– [which / whereis](#四which–whereis)
– [find 与 grep 组合](#五find-与-grep-组合)
—
## 一、find 命令
按文件名、类型、大小、时间等条件查找文件。
### 基本用法
“`bash
# 按文件名查找
find . -name “moudle.c”
find . -name “*.c”
find . -name “*.ko”
# 忽略大小写
find /home -iname “makefile”
# 查找指定目录下的文件
find /home/eh/linux -name “Makefile”
“`
### 按时间查找
“`bash
# 最近 7 天修改过的文件
find . -mtime -7
# 正好 7 天前修改的文件
find . -mtime 7
# 超过 7 天未修改的文件
find . -mtime +7
# 最近 1 天内访问过的文件
find . -atime -1
# 最近 1 分钟内修改过的文件
find . -mmin -1
“`
### 按类型和大小查找
“`bash
# 查找目录
find . -type d -name “test”
# 查找普通文件
find . -type f -name “*.c”
# 查找大于 10MB 的文件
find . -size +10M
# 查找小于 1KB 的文件
find . -size -1k
“`
### find 的高级选项
“`bash
# 排除某个目录
find . -path “./build” -prune -o -name “*.c” -print
# 查找后执行命令(\; 表示每个文件单独执行一次)
find . -name “*.c” -exec grep “hello” {} \;
# 查找后显示文件名(-l 只显示文件名)
find . -name “*.c” -exec grep -l “hello” {} \;
# 查找后显示行号(-H 显示文件名,-n 显示行号)
find . -name “*.c” -exec grep -Hn “hello” {} \;
# 查找后删除所有 .tmp 文件(慎用)
find . -name “*.tmp” -exec rm {} \;
“`
—
## 二、grep 命令
按文件内容搜索,是开发中最常用的命令。
### 基本用法
“`bash
# 在文件中搜索包含 “hello” 的行
grep “hello” moudle.c
# 递归搜索子目录(-r)
grep -r “printk” .
# 显示行号(-n)
grep -rn “module_init” .
# 只显示文件名,不显示匹配行(-l 大写)
grep -rl “GPL” *.c
“`
### 常用选项
“`bash
# 忽略大小写(-i)
grep -ri “hello” .
# 显示匹配行的前后行(-C)
grep -C 3 “hello_init” moudle.c
# 只显示匹配的行数(-c)
grep -c “printk” moudle.c
# 反向搜索,显示不包含关键词的行(-v)
grep -v “test” file.c
# 使用正则表达式(-E)
grep -E “^#include|static” moudle.c
# 显示匹配行之前 2 行(-B)
grep -B 2 “module_init” moudle.c
# 显示匹配行之后 2 行(-A)
grep -A 2 “module_init” moudle.c
“`
### grep 与文件类型结合
“`bash
# 搜索所有 .c 文件(–include)
grep -rn “alloc_chrdev_region” –include=”*.c” .
# 搜索所有 .c 和 .h 文件
grep -rn “module_init” –include=”*.c” –include=”*.h” .
# 排除某些目录
grep -rn “printk” . –exclude-dir=”build”
# 排除 test 和 docs 目录
grep -rn “alloc” –include=”*.c” . –exclude-dir=”test” –exclude-dir=”docs”
“`
### grep 实用技巧
“`bash
# 统计关键字出现次数
grep -c “printk” moudle.c
# 高亮显示匹配内容(–color)
grep –color “hello” moudle.c
# 搜索多个关键词(任一匹配)
grep -E “error|warning|fail” log.txt
# 搜索多个关键词(必须同时匹配)
grep “error” log.txt | grep “warning”
# 搜索空行
grep -n “^$” moudle.c
# 搜索以 # 开头的行(注释行)
grep -n “^#” moudle.c
“`
—
## 三、locate 命令
基于预建数据库的快速文件搜索工具,速度极快。
“`bash
# 首次使用需要更新数据库
sudo updatedb
# 快速查找任何包含关键词的文件
locate moudle.c
locate Makefile
# 查找包含路径的文件
locate “*char_driver*”
“`
> 注意:新建文件后需要 `sudo updatedb` 更新数据库才能查到。
—
## 四、which / whereis
“`bash
# 查找命令的完整路径(只在 PATH 中搜索)
which gcc
which make
which arm-linux-gnueabihf-gcc
# 查找命令的二进制、源码、man 手册位置
whereis gcc
whereis make
# 查找所有相关文件
whereis -a gcc
“`
—
## 五、find 与 grep 组合
### 方式一:管道 + xargs
“`bash
# 查找所有 .c 文件,交由 grep 处理
find . -name “*.c” | xargs grep “hello”
# 查找 .c 和 .h 文件
find . \( -name “*.c” -o -name “*.h” \) | xargs grep “module_init”
# 查找包含关键词的 .c 文件,并显示行号
find . -name “*.c” | xargs grep -Hn “printk”
“`
### 方式二:find -exec
“`bash
# 查找并执行 grep(每个文件单独执行一次)
find . -name “*.c” -exec grep “hello” {} \;
# 查找并显示文件名
find . -name “*.c” -exec grep -l “hello” {} \;
# 查找并显示文件名和行号
find . -name “*.c” -exec grep -Hn “hello” {} \;
“`
### 方式三:find -exec vs xargs 对比
“`bash
# xargs:先收集所有文件名,再统一执行一次 grep(效率更高)
find . -name “*.c” | xargs grep “hello”
# -exec:每个文件执行一次 grep(文件多时效率低)
find . -name “*.c” -exec grep “hello” {} \;
# xargs 更好的写法(文件名含空格时用 -print0)
find . -name “*.c” -print0 | xargs -0 grep “hello”
“`
—
## 六、实际开发案例
### 驱动开发场景
“`bash
# 在内核目录搜索所有驱动中包含 GPIO 的内容
find /home/eh/linux/…/linux-imx -name “*.c” | xargs grep -rn “gpio”
# 搜索所有 Makefile 中包含 obj-m 的内容
find . -name “Makefile” | xargs grep “obj-m”
# 在内核源码中搜索配置选项
grep -rn “CONFIG_MXC” .config
# 查找所有 .c 文件并统计行数
find . -name “*.c” -exec wc -l {} \;
“`
### 搜索并替换
“`bash
# 批量替换文件中的字符串
find . -name “*.c” | xargs sed -i ‘s/old_function/new_function/g’
# 备份并替换
find . -name “*.txt” -exec sed -i.bak ‘s/foo/bar/g’ {} \;
“`
—
## 七、速查表
| 命令 | 用途 | 示例 |
|——|——|——|
| `find -name` | 按文件名查找 | `find . -name “*.c”` |
| `find -mtime` | 按修改时间查找 | `find . -mtime -7` |
| `find -size` | 按文件大小查找 | `find . -size +10M` |
| `find -exec` | 查找后执行命令 | `find . -exec rm {} \;` |
| `grep -r` | 递归搜索内容 | `grep -rn “printk” .` |
| `grep -l` | 只显示文件名 | `grep -rl “hello” .` |
| `grep -n` | 显示行号 | `grep -rn “hello” .` |
| `grep -i` | 忽略大小写 | `grep -ri “hello” .` |
| `grep -C` | 显示上下文 | `grep -C 3 “hello” file` |
| `grep -v` | 反向搜索 | `grep -v “test” file` |
| `grep –include` | 指定文件类型 | `grep -rn “x” –include=”*.c”` |
| `locate` | 快速文件搜索 | `locate moudle.c` |
| `which` | 查找命令路径 | `which gcc` |
| `xargs` | 组合命令 | `find . \| xargs grep “x”` |