Linux文件搜索实用指南
一、按文件名与路径查找
find /path -name "*.log"、find /path -iname "readme*"find /path -type f(普通文件)、d(目录)、l(符号链接)find /path -size +100M(大于100MB)、-10k(小于10KB)find /path -mtime -7(近7天修改)、+30(30天前修改)find /var -type f -name "*.conf" -size +10k -mtime -30find . -name "*.tmp" -delete 或 find . -name "*.txt" -exec ls -lh {} \;locate nginx.conflocate -i "ReadMe.md"sudo updatedb(新文件未入库时可能搜不到;如**/tmp**通常不在收录范围)二、在文件内容中搜索关键字
grep -r "error" /var/loggrep -rin "timeout" ./config/grep -rl "success" /home/user/docsgrep -C 2 "panic" syslog(前后各2行)、-A 3(后3行)、-B 3(前3行)grep -E "GET|POST" access.logfind /var/log -type f -name "*.log" | xargs grep -l "fail"find . -maxdepth 2 -type f -name "*.conf" -exec grep -n "port" {} +rg "TODO" src/。-v反向匹配、-o仅输出匹配片段。三、定位命令与文档路径
which python3。whereis gcc;可用 -b/-m/-s 限定只查二进制/手册/源码。四、实用组合与性能建议
find . -path "./node_modules" -prune -o -name "*.js" -printfind . -maxdepth 3 -type f -name "*.py"find . -type f -name "*.log" -print0 | xargs -0 -P 4 grep -H "ERROR"(按0字节分隔,提升含空格路径安全性与并行度)find . -type f \( -name "*.c" -o -name "*.h" \) -exec grep -n "TODO" {} +find . -name "*.bak" -exec ls -l {} \;(确认无误再替换为 -delete)