string 如何使用 grep 在文件夹中查找单词?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4121803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:52:54  来源:igfitidea点击:

How can I use grep to find a word inside a folder?

stringcommand-linegrepkeyword-searchfile-search

提问by kiki

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.

在 Windows 中,我会搜索文件夹中的单词。同样,我想知道一个特定的词是否出现在包含许多子目录和文件的目录中。我对 grep 语法的搜索显示我必须指定文件名,即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).

现在,我不知道文件名,那我该怎么办?一个朋友建议这样做grep -nr string,但我不知道这是什么意思,我没有得到任何结果(在我发出 a 之前没有回应ctrl + c)。

回答by Manish Ranjan

grep -nr 'yourString*' .

The dot at the end searches the current directory. Meaning for each parameter:

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

-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*' .(Would find MobileAppServlet.javaor MobileAppServlet.classor MobileAppServlet.txt; 'MobileAppASer*.*'is another way to do the same thing.)

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

To check more parameters use man grepcommand.

要检查更多参数,请使用man grep命令。

回答by usta

grep -nr string my_directory

Additional notes: this satisfies the syntax grep [options] string filenamebecause 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 [options] string filename因为在类 Unix 系统中,目录是一种文件(有一个术语“常规文件”专门指代在 Windows 中仅称为“文件”的实体)。

grep -nr stringreads 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 -nr string从标准输入读取要搜索的内容,这就是为什么它只是在那里等待您的输入,并在您按下 ^C 时停止这样做(它也会在 ^D 上停止,这是结束的组合键-文件)。

回答by Stefan Steiger

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.

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

grep -R "your word" .

-nwill print the line number, where it matched in the file.
-iwill search case-insensitive (capital/non-capital letters).

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

grep -inR "your regex pattern" .

回答by Nico Huysamen

grep -nr search_string search_dir

will do a RECURSIVE (meaning the directory and all it's sub-directories) search for the search_string. (as correctly answered by usta).

将对 search_string 进行递归(意味着目录及其所有子目录)搜索。(正如usta正确回答的那样)。

The reason you were not getting any anwers with your friend's suggestion of:

你没有得到朋友的建议的原因是:

grep -nr string

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:

是因为没有指定目录。如果您在要搜索的目录中,则必须执行以下操作:

grep -nr string .

It is important to include the '.' character, as this tells grep to search THIS directory.

包含“.”很重要。字符,因为这告诉 grep 搜索这个目录。

回答by mu is too short

There's also:

还有:

find directory_name -type f -print0 | xargs -0 grep -li word

but that might be a bit much for a beginner.

但这对初学者来说可能有点多。

findis a general purpose directory walker/lister, -type fmeans "look for plain files rather than directories and named pipes and what have you", -print0means "print them on the standard output using null characters as delimiters". The output from findis sent to xargs -0and 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 the applies grep -li wordto each set of files. On the grep, -lmeans "list the files that match" and -imeans "case insensitive"; you can usually combine single character options so you'll see -limore often than -l -i.

find是一个通用目录 walker/lister,-type f意思是“查找普通文件而不是目录和命名管道以及你有什么”,-print0意思是“使用空字符作为分隔符在标准输出上打印它们”。的输出find被发送到xargs -0并使用空字符作为记录分隔符(而不是标准的换行符)并应用于grep -li word每组文件以块的形式获取其标准输入(以避免命令行长度限制)。在grep,-l表示“列出匹配的文件”,-i表示“不区分大小写”;您通常可以组合单个字符选项,这样您会看到-li-l -i.

If you don't use -print0and -0then you'll run into problems with file names that contain spaces so using them is a good habit.

如果你不使用-print0,并-0然后你会碰到与包含空格,以便使用它们是一个好习惯文件名的问题。

回答by Paxwell

Why not do a recursive search to find all instances in sub directories:

为什么不进行递归搜索以查找子目录中的所有实例:

grep -r 'text' *

This works like a charm.

这就像一个魅力。

回答by Nwaoga

  1. grep -r "yourstring" *Will find "yourstring" in any files and folders
  1. grep -r "yourstring" *将在任何文件和文件夹中找到“yourstring”

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

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

  1. grep -rE "yourstring|yourotherstring|$" *will search for list locations where yourstringor yourotherstringmatches
  1. grep -rE "yourstring|yourotherstring|$" *将搜索yourstringyourotherstring匹配的列表位置

回答by jfhfhf839

Another option that I like to use:

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

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

-type f returns you only files and not folders

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

-exec and {} runs the grep on the files that were found in the search (the exact syntax is "-exec command {}").

-exec 和 {} 对搜索中找到的文件运行 grep(确切的语法是“-exec command {}”)。

回答by DimiDak

grep -R "string" /directory/

-R follows also symlinks when -r does not.

-R 也跟随符号链接,当 -r 没有时。

回答by eLRuLL

The answer you selected is fine, and it works, but it isn't the correct way to do it, because:

您选择的答案很好,并且有效,但这不是正确的方法,因为:

grep -nr yourString* .

This actually searches the string "yourStrin"and "g"0 or many times.

这实际上搜索了字符串"yourStrin""g"0 次或多次。

So the proper way to do it is:

所以正确的做法是:

grep -nr \w*yourString\w* .

This command searches the string with any character before and after on the current folder.

该命令搜索当前文件夹前后任意字符的字符串。