在Windows中,我会在文件夹中搜索一个单词。类似地,我想知道特定的单词是否出现在包含许多子目录和文件的目录中。我对grep语法的搜索显示我必须指定文件名,即grep字符串文件名。
现在,我不知道文件名,我该怎么做? 一个朋友建议做grep -nr字符串,但我不知道这意味着什么,我没有得到任何结果(没有响应,直到我发出Ctrl + C)。
在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 -nr string my_directory
附加说明:这满足grep [options] string filename语法,因为在类unix系统中,目录是一种文件(有一个术语“常规文件”专门指在Windows中被称为“文件”的实体)。
grep -nr string从标准输入读取内容进行搜索,这就是为什么它只是等待你的输入,并在你按^C时停止这样做(它也会在^D时停止,这是文件结束的组合键)。
不要使用grep。下载Silver Searcher或ripgrep。它们都很出色,并且比grep或ack快得多,有大量的选项。
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命令。
为什么不做一个递归搜索来找到子目录中的所有实例:
grep -r 'text' *
这就像一个咒语。