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
。
grep -ri --include=*.txt "sometext" *
--include
标志需要用引号括起来的文件匹配模式,否则它将不能正常工作。正确的使用方式是 --include="*.txt"
。请注意,不要改变原始意思。 - tralph3find . -type f -exec bash -c "file -bi {} | grep -q text" \; -exec grep TEXTSEARCH {} ";"