我如何在其文件内容中找到包含一个特定的文本序列的所有文件?

下一个不起作用. 它似乎显示系统中的每个单一文件。

find / -type f -exec grep -H 'text-to-find-here' {} \;

当前回答

您可以使用 ack. 它就像输入源代码. 您可以扫描您的整个文件系统。

只是做:

ack 'text-to-find-here'

在你的根目录中。

您也可以使用常规表达式,指定文件类型等。


更新

我刚刚发现The Silver Searcher,这类似于Ack,但比它快3至5倍,甚至忽略了来自.gitignore 文件的模式。

其他回答

要搜索字符串和输出,就是与搜索字符串相同的字符串:

for i in $(find /path/of/target/directory -type f); do grep -i "the string to look for" "$i"; done

吉:

for i in $(find /usr/share/applications -type f); \
do grep -i "web browser" "$i"; done

要显示包含搜索字符串的文件名:

for i in $(find /path/of/target/directory -type f); do if grep -i "the string to look for" "$i" > /dev/null; then echo "$i"; fi; done;

吉:

for i in $(find /usr/share/applications -type f); \
do if grep -i "web browser" "$i" > /dev/null; then echo "$i"; \
fi; done;

试试这个命令,它会给你包含你输入的模式的文件。

sudo grep -inr "your-pattern" /

在这里:i - 忽略案例区别,以便只有在案例中区别的字符相匹配。

n - 确保实际行内容的第一个字符在一张标签站上,以便标签的调整看起来正常。

r - 阅读每个目录下的所有文件,重复,仅按照符号链接,如果它们在命令线上。

这个捕捉命令会给你一个准确的结果,当你正在寻找特定的文本在Linux -

grep -inRsH “文本要被搜索” /path/to/dir(它可以是“。

i stands for ignore case distinctions R stands for recursive and it also includes symlinks. It is better to use 'R' instead of 'r' n stands for "it will print line number." s stands for "suppress error messages" H stands for "it will print the file name for each match"

你可以使用:

grep -r "string to be searched"  /path/to/dir

r 是可重复的,因此将在指定的路径和其子指南中进行搜索,这将告诉您文件名以及在文件中印刷线的字符串。

或类似于您正在尝试的命令(例如: )在所有JavaScript文件中搜索( (*.js):

find . -name '*.js' -exec grep -i 'string to search for' {} \; -print

这将打印文本显示的文件中的行,但它不会打印文件名。

除了此命令之外,我们还可以写下: grep -rn “String to search” /path/to/directory/or/file -r: recursive search n: line number will be shown for matches

您可以使用 ack. 它就像输入源代码. 您可以扫描您的整个文件系统。

只是做:

ack 'text-to-find-here'

在你的根目录中。

您也可以使用常规表达式,指定文件类型等。


更新

我刚刚发现The Silver Searcher,这类似于Ack,但比它快3至5倍,甚至忽略了来自.gitignore 文件的模式。