在Windows中,我会在文件夹中搜索一个单词。类似地,我想知道特定的单词是否出现在包含许多子目录和文件的目录中。我对grep语法的搜索显示我必须指定文件名,即grep字符串文件名。

现在,我不知道文件名,我该怎么做? 一个朋友建议做grep -nr字符串,但我不知道这意味着什么,我没有得到任何结果(没有响应,直到我发出Ctrl + C)。


当前回答

grep -nr 'yourString*' .

末尾的点搜索当前目录。每个参数的含义:

-n            Show relative line number in the file
'yourString*' String for search, followed by a wildcard character
-r            Recursively search subdirectories listed
.             Directory for search (current directory)

grep -nr 'MobileAppSer*'。(会找到MobileAppServlet.java或MobileAppServlet.class或MobileAppServlet.txt;“MobileAppASer *。*'是做同样事情的另一种方式。)

需要查询更多参数,使用man grep命令。

其他回答

我喜欢使用的另一个选项:

find folder_name -type f -exec grep your_text  {} \;

-type f只返回文件而不返回文件夹

-exec and{}在搜索中找到的文件上运行grep(准确的语法是“-exec command{}”)。

Grep -r "yourstring" * 会发现“你的字符串”在任何文件和文件夹

现在,如果你想同时查找两个不同的字符串,你可以使用选项E并为搜索添加单词。休息之后的例子

grep -rE "yourstring|yourotherstring|$" *将搜索yourstring或yourotherstring匹配的列表位置

grep -nr 'yourString*' .

末尾的点搜索当前目录。每个参数的含义:

-n            Show relative line number in the file
'yourString*' String for search, followed by a wildcard character
-r            Recursively search subdirectories listed
.             Directory for search (current directory)

grep -nr 'MobileAppSer*'。(会找到MobileAppServlet.java或MobileAppServlet.class或MobileAppServlet.txt;“MobileAppASer *。*'是做同样事情的另一种方式。)

需要查询更多参数,使用man grep命令。

类似于@eLRuLL发布的答案,一个更简单的方法来指定一个尊重单词边界的搜索是使用-w选项:

grep -wnr "yourString" .

GREP:全局正则表达式打印/解析器/处理器/程序。 您可以使用它来搜索当前目录。 你可以为“递归”指定-R,这意味着程序在所有子文件夹中搜索,以及它们的子文件夹,以及它们的子文件夹的子文件夹,等等。

grep -R "your word" .

-n将打印行号,即它在文件中匹配的行号。 -i将搜索不区分大小写(大写/非大写字母)。

grep -inR "your regex pattern" .