【发布时间】:2012-04-06 02:37:02
【问题描述】:
find . -type f | xargs file | grep text | cut -d':' -f1 | xargs grep -l "TEXTSEARCH" {}
这是一个很好的解决方案?仅在文本文件中递归查找 TEXTSEARCH
【问题讨论】:
find . -type f | xargs file | grep text | cut -d':' -f1 | xargs grep -l "TEXTSEARCH" {}
这是一个很好的解决方案?仅在文本文件中递归查找 TEXTSEARCH
【问题讨论】:
您可以在grep 中使用-r(递归)和-I(忽略二进制)选项:
$ grep -rI "TEXTSEARCH" .
-I处理二进制文件,就好像它不包含匹配数据一样;这相当于--binary-files=without-match选项。-r递归读取每个目录下的所有文件;这相当于-d recurse选项。
【讨论】:
另一个比 kevs 更不优雅的解决方案是将 find 中的 -exec 命令链接在一起,没有 xargs 和 cut:
find . -type f -exec bash -c "file -bi {} | grep -q text" \; -exec grep TEXTSEARCH {} ";"
【讨论】:
-I
如果您知道要搜索的文件扩展名是什么,那么一种非常简单的方法可以从当前目录中搜索所有 *.txt 文件,递归遍历所有子目录,不区分大小写:
grep -ri --include=*.txt "sometext" *
【讨论】:
--include 标志需要用引号括起来的全局模式。这是行不通的,更像这样:--include="*.txt".