如何使用 grep 查找文件夹内的单词?
在 Windows 中,我会进行搜索以在文件夹中查找单词。同样,我想知道某个特定单词是否出现在包含许多子目录和文件的目录中。我对 grep 语法的搜索显示我必须指定文件名,即 grep string filename
。
现在,我不知道文件名,那么我该怎么办? 一位朋友建议执行 grep -nr string
,但我不知道这意味着什么,并且没有得到任何结果(直到我发出 Ctrl 后才得到响应) + C)。
In Windows, I would have done a search for finding a word inside a folder. Similarly, I want to know if a specific word occurs inside a directory containing many sub-directories and files. My searches for grep syntax shows I must specify the filename, i.e. grep string filename
.
Now, I do not know the filename, so what do I do?
A friend suggested to do grep -nr string
, but I don't know what this means and I got no results with it (there is no response until I issue a Ctrl + C).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
末尾的点搜索当前目录。每个参数的含义:
grep -nr 'MobileAppSer*' .
(会查找MobileAppServlet.java
或MobileAppServlet.class
或MobileAppServlet. txt
;'MobileAppASer*.*'
是执行相同操作的另一种方法。)要检查更多参数,请使用 man grep 命令。
The dot at the end searches the current directory. Meaning for each parameter:
grep -nr 'MobileAppSer*' .
(Would findMobileAppServlet.java
orMobileAppServlet.class
orMobileAppServlet.txt
;'MobileAppASer*.*'
is another way to do the same thing.)To check more parameters use man grep command.
附加说明:这满足语法
grep [options] string filename
因为在类 Unix 系统中,目录是一种文件(有一个术语“常规文件”专门指代实体)在 Windows 中仅称为“文件”)。grep -nr string
从标准输入中读取要搜索的内容,这就是为什么它只是等待您的输入,并在您按 ^C 时停止这样做(它会在 ^D 上停止,因为好吧,这是文件结束的组合键)。Additional notes: this satisfies the syntax
grep [options] string filename
because in Unix-like systems, a directory is a kind of file (there is a term "regular file" to specifically refer to entities that are called just "files" in Windows).grep -nr string
reads the content to search from the standard input, that is why it just waits there for input from you, and stops doing so when you press ^C (it would stop on ^D as well, which is the key combination for end-of-file).GREP:全局正则表达式打印/解析器/处理器/程序。
您可以使用它来搜索当前目录。
您可以指定 -R 表示“递归”,这意味着程序将搜索所有子文件夹及其子文件夹及其子文件夹的子文件夹等。
-n
将打印行号,其中在文件中匹配。-i
将搜索不区分大小写(大写/非大写字母)。GREP: Global Regular Expression Print/Parser/Processor/Program.
You can use this to search the current directory.
You can specify -R for "recursive", which means the program searches in all subfolders, and their subfolders, and their subfolder's subfolders, etc.
-n
will print the line number, where it matched in the file.-i
will search case-insensitive (capital/non-capital letters).还有:
但这对于初学者来说可能有点太多了。
find
是通用目录 walker/lister,-type f
表示“查找普通文件而不是目录和命名管道之类的”,-print0
表示“使用空字符作为分隔符将它们打印在标准输出上”。find
的输出被发送到xargs -0
并使用空字符作为记录分隔符(而不是标准换行符),然后将grep -li word
应用于每组文件。在grep
中,-l
表示“列出匹配的文件”,-i
表示“不区分大小写”;通常,您可以组合单个字符选项,这样您会比-l -i
更频繁地看到-li
。如果您不使用
-print0
和-0
那么您将遇到包含空格的文件名问题,因此使用它们是一个好习惯。There's also:
but that might be a bit much for a beginner.
find
is a general purpose directory walker/lister,-type f
means "look for plain files rather than directories and named pipes and what have you",-print0
means "print them on the standard output using null characters as delimiters". The output fromfind
is sent toxargs -0
and that grabs its standard input in chunks (to avoid command line length limitations) using null characters as a record separator (rather than the standard newline) and then appliesgrep -li word
to each set of files. On thegrep
,-l
means "list the files that match" and-i
means "case insensitive"; you can usually combine single character options so you'll see-li
more often than-l -i
.If you don't use
-print0
and-0
then you'll run into problems with file names that contain spaces so using them is a good habit.将对 search_string 进行递归(即目录及其所有子目录)搜索。 (usta 正确回答)。
您没有收到朋友建议的任何答复的原因
是:没有指定目录。如果您位于要进行搜索的目录中,则必须执行以下操作:
包含“.”非常重要。字符,因为这告诉 grep 搜索此目录。
will do a RECURSIVE (meaning the directory and all it's sub-directories) search for the search_string. (as correctly answered by usta).
The reason you were not getting any anwers with your friend's suggestion of:
is because no directory was specified. If you are in the directory that you want to do the search in, you have to do the following:
It is important to include the '.' character, as this tells grep to search THIS directory.
为什么不进行递归搜索来查找子目录中的所有实例:
这就像一个魅力。
Why not do a recursive search to find all instances in sub directories:
This works like a charm.
与 @eLRuLL 发布的答案类似,指定尊重单词边界的搜索的更简单方法是使用
-w
选项:Similar to the answer posted by @eLRuLL, an easier way to specify a search that respects word boundaries is to use the
-w
option:我喜欢使用的另一个选项:
-type f 仅返回文件而不是文件夹
-exec 和 {} 对搜索中找到的文件运行 grep (确切的语法是“-exec command {}”)。
Another option that I like to use:
-type f returns you only files and not folders
-exec and {} runs the grep on the files that were found in the search (the exact syntax is "-exec command {}").
grep -r“你的字符串”*
将在任何文件和文件夹中找到“yourstring”
现在,如果您想同时查找两个不同的字符串,您可以随时使用选项 E 并添加搜索词。中断后的示例
grep -rE "yourstring|yourotherstring|$" *
将搜索yourstring
或yourotherstring
匹配的列表位置grep -r "yourstring" *
Will find "yourstring" in any files and folders
Now if you want to look for two different strings at the same time you can always use option E and add words for the search. example after the break
grep -rE "yourstring|yourotherstring|$" *
will search for list locations whereyourstring
oryourotherstring
matches当 -r 不跟随时,-R 也跟随符号链接。
-R follows also symlinks when -r does not.
您选择的答案很好,并且有效,但这不是正确的方法,因为:
这实际上搜索字符串
"yourStrin"
和"g"
0 次或多次。因此,正确的方法是:
此命令在当前文件夹中搜索前后包含任何字符的字符串。
The answer you selected is fine, and it works, but it isn't the correct way to do it, because:
This actually searches the string
"yourStrin"
and"g"
0 or many times.So the proper way to do it is:
This command searches the string with any character before and after on the current folder.
以下示例在位于文件夹
path1< 内某处的
*.xml
和*.js
文件中递归查找您的搜索字符串
/code>、path2
和path3
。因此,您可以在文件的子集中搜索许多目录,只需在末尾提供路径即可。
The following sample looks recursively for
your search string
in the*.xml
and*.js
files located somewhere inside the folderspath1
,path2
andpath3
.So you can search in a subset of the files for many directories, just providing the paths at the end.
在目录内运行(终端)以下命令。它也会递归检查子目录内部。
Run(terminal) the following command inside the directory. It will recursively check inside subdirectories too.
将 ls 管道连接到 grep,这样您就可以在当前目录中搜索所需的任何内容。
-n 用于显示该行。
Piping ls to grep, so you can search whatever you need inside the directory you currently are.
-n is used to show the line.
不要使用 grep。下载 Silver Searcher 或 ripgrep。它们都很出色,而且比 grep 或 ack 快得多,并且有大量选项。
Don't use grep. Download Silver Searcher or ripgrep. They're both outstanding, and way faster than grep or ack with tons of options.