如何仅在文本文件中搜索字符串?(递归)

Lan*_*nes 4 grep find

我可以列出文件夹中的所有文本文件(按 mimetype):

find . -type f -print0 | xargs -0 file -i | fgrep -i text | sed 's/:$//g' 2>/dev/null | awk 'BEGIN {FS=": "} {print $1}'
Run Code Online (Sandbox Code Playgroud)

好的。但是,我如何向其中添加“fgrep”,以在这些文件中搜索“STRING”(不需要正则表达式,这就是 fgrep 的原因)。

这不好:

fgrep -iR "STRING" *
Run Code Online (Sandbox Code Playgroud)

因为它开始在 ISO 文件中搜索,二进制文件也是......

Fedora14/bash。

roz*_*acz 11

首先grep: 您可以告诉它不要搜索二进制文件 - 使用-I开关 - 如联机帮助页所述:

-I     Process a binary  file  as  if  it  did  not  contain  matching data;
       this  is  equivalent  to  the --binary-files=without-match option.
Run Code Online (Sandbox Code Playgroud)

其次find:为了避免使用 xargs 和大量管道,请使用程序的-exec测试find。您可以使用此轻松创建一系列逻辑测试:如果所有先前的命令返回 0(成功完成),-exec则执行每个连续测试。