当前位置: 首页 > news >正文

Linux 查找文件指令总结

一、find(最强大、最常用)

基本语法

find [搜索路径] [匹配条件] [执行动作]

按名称查找

# 精确匹配文件名 find / -name "test.txt" # 忽略大小写 find /home -iname "Readme.md" # 通配符匹配 find /var -name "*.log" find /etc -name "nginx*" find /tmp -name "???.txt" # 匹配3个字符的文件名
# 精确匹配文件名 find / -name "test.txt" # 忽略大小写 find /home -iname "Readme.md" # 通配符匹配 find /var -name "*.log" find /etc -name "nginx*" find /tmp -name "???.txt" # 匹配3个字符的文件名

按类型查找

find / -type f -name "*.conf" # 普通文件 find / -type d -name "logs" # 目录 find / -type l -name "*.link" # 符号链接 find / -type b # 块设备 find / -type c # 字符设备 find / -type s # 套接字 find / -type p # 命名管道
find / -type f -name "*.conf" # 普通文件 find / -type d -name "logs" # 目录 find / -type l -name "*.link" # 符号链接 find / -type b # 块设备 find / -type c # 字符设备 find / -type s # 套接字 find / -type p # 命名管道

按大小查找

find / -size +100M # 大于 100MB find / -size -1M # 小于 1MB find / -size 50M # 恰好 50MB find / -size +1G -size -10G # 1GB ~ 10GB 之间 find / -empty # 空文件/空目录
find / -size +100M # 大于 100MB find / -size -1M # 小于 1MB find / -size 50M # 恰好 50MB find / -size +1G -size -10G # 1GB ~ 10GB 之间 find / -empty # 空文件/空目录

按时间查找

# 修改时间(内容修改) find / -mtime -7 # 7天内修改的 find / -mtime +30 # 30天前修改的 find / -mtime 1 # 恰好1天前(昨天) # 访问时间 find / -atime -1 # 1天内访问过的 # 变更时间(元数据变化) find / -ctime -7 # 7天内 # 按分钟(更精确) find / -mmin -30 # 30分钟内修改的 find / -mmin +60 # 60分钟前修改的
# 修改时间(内容修改) find / -mtime -7 # 7天内修改的 find / -mtime +30 # 30天前修改的 find / -mtime 1 # 恰好1天前(昨天) # 访问时间 find / -atime -1 # 1天内访问过的 # 变更时间(元数据变化) find / -ctime -7 # 7天内 # 按分钟(更精确) find / -mmin -30 # 30分钟内修改的 find / -mmin +60 # 60分钟前修改的

按权限/用户查找

find / -perm 755 # 权限恰好 755 find / -perm -644 # 至少包含 644 权限 find / -perm /u+w # 用户有写权限 find / -user john # 属主为 john find / -group www # 属组为 www find / -nouser # 无属主的文件 find / -nogroup # 无属组的文件
find / -perm 755 # 权限恰好 755 find / -perm -644 # 至少包含 644 权限 find / -perm /u+w # 用户有写权限 find / -user john # 属主为 john find / -group www # 属组为 www find / -nouser # 无属主的文件 find / -nogroup # 无属组的文件

按目录深度查找

find / -maxdepth 3 -name "*.py" # 最多搜索3层目录 find / -mindepth 2 -name "*.py" # 至少从第2层开始搜索

组合条件

# AND(默认) find / -name "*.log" -size +100M # OR find / -name "*.log" -o -name "*.txt" # NOT find / -name "*.log" ! -name "debug.log" # 复杂组合 find /var $$ -name "*.log" -o -name "*.tmp" $$ -size +10M find / -type f -name "*.bak" -mtime +90 ! -empty
# AND(默认) find / -name "*.log" -size +100M # OR find / -name "*.log" -o -name "*.txt" # NOT find / -name "*.log" ! -name "debug.log" # 复杂组合 find /var $$ -name "*.log" -o -name "*.tmp" $$ -size +10M find / -type f -name "*.bak" -mtime +90 ! -empty

执行动作

# 删除匹配的文件 find /tmp -name "*.tmp" -mtime +7 -delete # 执行命令({} 代表匹配的文件) find / -name "*.log" -exec gzip {} \; find / -name "*.log" -exec rm -f {} \; find / -name "*.conf" -exec grep -l "port" {} \; # 批量执行(效率更高,用 + 代替 \;) find / -name "*.log" -exec gzip {} + # 打印详细信息 find /home -name "*.sh" -ls # 确认后删除(交互式) find /tmp -name "*.tmp" -ok rm {} \;
# 删除匹配的文件 find /tmp -name "*.tmp" -mtime +7 -delete # 执行命令({} 代表匹配的文件) find / -name "*.log" -exec gzip {} \; find / -name "*.log" -exec rm -f {} \; find / -name "*.conf" -exec grep -l "port" {} \; # 批量执行(效率更高,用 + 代替 \;) find / -name "*.log" -exec gzip {} + # 打印详细信息 find /home -name "*.sh" -ls # 确认后删除(交互式) find /tmp -name "*.tmp" -ok rm {} \;

二、locate(快速全盘搜索)

# 安装 sudo yum install mlocate # CentOS sudo apt install plocate # Ubuntu # 更新数据库 sudo updatedb # 搜索 locate test.txt locate -i "readme.md" # 忽略大小写 locate -c "*.log" # 只显示数量 locate -l 10 "*.conf" # 限制结果数量 locate -r "nginx.*\.conf$" # 正则匹配
# 安装 sudo yum install mlocate # CentOS sudo apt install plocate # Ubuntu # 更新数据库 sudo updatedb # 搜索 locate test.txt locate -i "readme.md" # 忽略大小写 locate -c "*.log" # 只显示数量 locate -l 10 "*.conf" # 限制结果数量 locate -r "nginx.*\.conf$" # 正则匹配

对比 find:locate 搜索的是预建数据库,速度极快,但新文件需先updatedb


三、which / whereis / type

# 查找可执行文件路径(在 $PATH 中搜索) which nginx which python3 # 查找二进制文件、源码、手册页 whereis nginx # 输出: nginx: /usr/sbin/nginx /etc/nginx /usr/share/man/man8/nginx.8.gz # 查找命令类型(内置/别名/外部) type cd # cd is a shell builtin type ls # ls is aliased to 'ls --color=auto' type nginx # nginx is /usr/sbin/nginx
# 查找可执行文件路径(在 $PATH 中搜索) which nginx which python3 # 查找二进制文件、源码、手册页 whereis nginx # 输出: nginx: /usr/sbin/nginx /etc/nginx /usr/share/man/man8/nginx.8.gz # 查找命令类型(内置/别名/外部) type cd # cd is a shell builtin type ls # ls is aliased to 'ls --color=auto' type nginx # nginx is /usr/sbin/nginx

四、grep(在文件内容中搜索)

# 在文件中搜索文本 grep "error" /var/log/syslog grep -r "password" /etc/ # 递归搜索目录 grep -rn "TODO" /home/ # 显示行号 grep -rl "localhost" /etc/ # 只显示文件名 # 高级选项 grep -i "error" log.txt # 忽略大小写 grep -w "port" config.txt # 全词匹配 grep -c "error" log.txt # 统计匹配行数 grep -A 3 "error" log.txt # 显示匹配行后3行 grep -B 3 "error" log.txt # 显示匹配行前3行 grep -C 3 "error" log.txt # 显示匹配行前后各3行 grep -v "debug" log.txt # 反向匹配(排除) # 正则表达式 grep -E "error|warn|fatal" log.txt # 扩展正则 grep -P "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" log.txt # Perl 正则(IP地址)
# 在文件中搜索文本 grep "error" /var/log/syslog grep -r "password" /etc/ # 递归搜索目录 grep -rn "TODO" /home/ # 显示行号 grep -rl "localhost" /etc/ # 只显示文件名 # 高级选项 grep -i "error" log.txt # 忽略大小写 grep -w "port" config.txt # 全词匹配 grep -c "error" log.txt # 统计匹配行数 grep -A 3 "error" log.txt # 显示匹配行后3行 grep -B 3 "error" log.txt # 显示匹配行前3行 grep -C 3 "error" log.txt # 显示匹配行前后各3行 grep -v "debug" log.txt # 反向匹配(排除) # 正则表达式 grep -E "error|warn|fatal" log.txt # 扩展正则 grep -P "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" log.txt # Perl 正则(IP地址)

五、awk / sed(高级文本搜索)

# awk 按列/条件筛选 awk '/error/ {print $0}' /var/log/syslog awk '$3 > 1000' data.txt # 第3列大于1000 awk -F: '/bash$/ {print $1}' /etc/passwd # 搜索使用bash的用户 # sed 搜索并替换 sed -n '/error/p' log.txt # 打印匹配行 sed -n '100,200p' largefile.txt # 打印100~200行
# awk 按列/条件筛选 awk '/error/ {print $0}' /var/log/syslog awk '$3 > 1000' data.txt # 第3列大于1000 awk -F: '/bash$/ {print $1}' /etc/passwd # 搜索使用bash的用户 # sed 搜索并替换 sed -n '/error/p' log.txt # 打印匹配行 sed -n '100,200p' largefile.txt # 打印100~200行

六、其他实用查找工具

fd(find 的现代替代品)

# 安装 sudo apt install fd-find # Ubuntu sudo yum install fd-find # CentOS # 使用 fd "test" fd -e py # 按扩展名查找 .py 文件 fd -H ".bashrc" # 包含隐藏文件 fd -t d "log" # 只搜索目录
# 安装 sudo apt install fd-find # Ubuntu sudo yum install fd-find # CentOS # 使用 fd "test" fd -e py # 按扩展名查找 .py 文件 fd -H ".bashrc" # 包含隐藏文件 fd -t d "log" # 只搜索目录

fzf(模糊搜索)

# 安装 sudo apt install fzf # 交互式搜索 fzf # 搜索当前目录 find / -name "*.log" | fzf # 管道交互式筛选
# 安装 sudo apt install fzf # 交互式搜索 fzf # 搜索当前目录 find / -name "*.log" | fzf # 管道交互式筛选

stat(查看文件详细元数据)

stat /etc/nginx/nginx.conf
stat /etc/nginx/nginx.conf

file(判断文件类型)

file /tmp/mystery.dat file -b /tmp/mystery.dat # 只输出类型描述
file /tmp/mystery.dat file -b /tmp/mystery.dat # 只输出类型描述

七、常用实战场景速查

场景命令
查找大于 1G 的文件find / -type f -size +1G 2>/dev/null
查找最近 1 天修改的文件find /var -type f -mtime -1
删除 7 天前的日志find /var/log -name "*.log" -mtime +7 -delete
查找权限为 777 的文件find / -perm 0777 -type f
查找属主为 root 的 SUID 文件find / -user root -perm -4000
查找并统计数量find / -name "*.conf" | wc -l
查找空文件/目录find / -empty -type f
查找被删除但仍占空间的文件lsof | grep deleted
查找超过 100M 的文件并排序find / -size +100M -exec ls -lh {} + 2>/dev/null | sort -k5 -h
查找两个文件间的差异diff file1 file2
快速定位可执行文件which nginx
内容搜索含 IP 的行grep -rnE "[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}" /var/log/
http://www.jsqmd.com/news/1246427/

相关文章:

  • GLM-5.2、K3、Qwen3.8-Max-Preview 大比拼,谁能在网站改版测试中笑到最后?
  • 2026年最新教程:手机上怎么把照片改成一寸照片?亲测可用方法 - 图片处理研究员
  • 2026 Codex 完整实战:从安装配置到 AGENTS.md、Skills 与 MCP
  • 宇舶佛山2026年7月最新网点地址及售后服务热线信息公示! - 亨得利钟表维修中心
  • Elasticsearch安装最新最全快速保姆级教程!!!
  • Unity项目集成NuGet包管理:原理、方案与实战避坑指南
  • 2026年小程序开发选山东慧兴网络科技
  • 机器学习在数据科学中的核心应用与工程实践
  • Hadoop+Spark+Kafka构建智能风控系统:从规则引擎到机器学习
  • 手机内存小缓存视频攻略:省内存离线缓存双管齐下
  • C++多进程编程深度解析:从fork到IPC实战应用
  • 美诚科技线索挖掘精准度行业实测对比
  • HNSWLib实战指南:C++向量检索库的原理、调优与避坑
  • 2026 年新消息:天心知名的环卫休息室岗亭供应厂家哪家可靠,别再浪费钱!这套岗亭如何帮你省下每月几千块?-裕盛岗亭厂家 - 领域鉴赏官
  • HarmonyOS ArkTS 实战:实现一个校园超市线上购物配送应用
  • RAG系统构建实战:多格式文档加载与智能分割技术
  • HN 禁止 AI 评论获 4229 赞|开源 AI 路线白热化,Flawless 与 vox-director 登榜|格式修复验证
  • VC++通过COM操作Excel:原理、代码实战与性能优化
  • Python脚本转C++程序的高效方法与实战指南
  • 【2026年7月】分享10款亲测好用的AI小说工具(内含优缺点对比图)
  • Unity项目迁移鸿蒙实战:五大核心挑战与解决方案全解析
  • Claude AI核心技术解析与应用实践指南
  • C++内存池设计与实现:从原理到高性能多线程优化
  • 从网络主播到影视出品人:魏小也发文澄清感情状态,专注深耕影视赛道
  • AI Agent 到底是什么?能干什么,怎么实现?
  • 从零学会规范写 Prompt|LangChain 双模板详解 + 3 套可复用业务案例源码
  • 2026 年现阶段扎兰屯正规的无机纤维喷涂优质厂家哪家好,揭秘:这套技术如何让材料成本骤降30%? - 鉴选官
  • C++模板类实战:从零手搓MyVector动态数组
  • Chrome启动参数详解:提升开发调试效率的实用指南
  • zcmd 3.8.10.11版本发布:医疗数据处理与FHIR交互新特性