温馨提示×

linux怎样查找特定文件

小樊
39
2026-09-01 15:20:28
栏目: 智能运维

在 Linux 中查找特定文件,常用的方法有以下几种,按使用频率从高到低介绍:


1. find 命令(最常用、最强大)

根据文件名、类型、大小、时间等条件查找。

按文件名查找

find /path -name "filename"

示例:在 /home 下查找名为 test.txt 的文件

find /home -name "test.txt"

忽略大小写

find /home -iname "test.txt"

使用通配符

find /home -name "*.log"

按文件类型

find /home -type f   # 普通文件
find /home -type d   # 目录

按大小

find /home -size +100M   # 大于100M
find /home -size -10k   # 小于10k

2. locate 命令(最快,但非实时)

基于数据库查找,速度快。

locate filename

⚠️ 如果找不到新文件,先更新数据库:

sudo updatedb

3. grep(查找文件内容,不是文件名)

如果你想找包含某内容的文件

grep -r "关键词" /path

4. which / whereis(找命令文件)

which python
whereis python

常见组合示例

  • 查找所有 .conf 文件:
find / -name "*.conf" 2>/dev/null
  • 查找 7 天内修改过的文件:
find /home -mtime -7

如果你能说明:

  • 是找文件名还是文件内容
  • 大概在哪个目录
  • 是否知道部分文件名

我可以给你更精确的命令。

0