如何使用 grep 查找文件夹内的单词?

发布于 2024-09-30 17:29:02 字数 264 浏览 4 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(15

败给现实 2024-10-07 17:29:02
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.javaMobileAppServlet.classMobileAppServlet. txt; 'MobileAppASer*.*' 是执行相同操作的另一种方法。)

要检查更多参数,请使用 ma​​n grep 命令。

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.java or MobileAppServlet.class or MobileAppServlet.txt; 'MobileAppASer*.*' is another way to do the same thing.)

To check more parameters use man grep command.

∞觅青森が 2024-10-07 17:29:02
grep -nr string my_directory

附加说明:这满足语法 grep [options] string filename 因为在类 Unix 系统中,目录是一种文件(有一个术语“常规文件”专门指代实体)在 Windows 中仅称为“文件”)。

grep -nr string 从标准输入中读取要搜索的内容,这就是为什么它只是等待您的输入,并在您按 ^C 时停止这样做(它会在 ^D 上停止,因为好吧,这是文件结束的组合键)。

grep -nr string my_directory

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).

拥有 2024-10-07 17:29:02

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

grep -R "your word" .

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

grep -inR "your regex pattern" .

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 "your word" .

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

grep -inR "your regex pattern" .
那请放手 2024-10-07 17:29:02

还有:

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

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

find 是通用目录 walker/lister,-type f 表示“查找普通文件而不是目录和命名管道之类的”,-print0 表示“使用空字符作为分隔符将它们打印在标准输出上”。 find 的输出被发送到 xargs -0 并使用空字符作为记录分隔符(而不是标准换行符),然后将 grep -li word 应用于每组文件。在 grep 中,-l 表示“列出匹配的文件”,-i 表示“不区分大小写”;通常,您可以组合单个字符选项,这样您会比 -l -i 更频繁地看到 -li

如果您不使用 -print0-0 那么您将遇到包含空格的文件名问题,因此使用它们是一个好习惯。

There's also:

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

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 from find is sent to xargs -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 applies grep -li word to each set of files. On the grep, -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.

毁虫ゝ 2024-10-07 17:29:02
grep -nr search_string search_dir

将对 search_string 进行递归(即目录及其所有子目录)搜索。 (usta 正确回答)。

您没有收到朋友建议的任何答复的原因

grep -nr string

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

grep -nr string .

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

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).

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.

转角预定愛 2024-10-07 17:29:02

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

grep -r 'text' *

这就像一个魅力。

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

grep -r 'text' *

This works like a charm.

只等公子 2024-10-07 17:29:02

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

grep -wnr "yourString" .

Similar to the answer posted by @eLRuLL, an easier way to specify a search that respects word boundaries is to use the -w option:

grep -wnr "yourString" .
紫轩蝶泪 2024-10-07 17:29:02

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

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

-type f 仅返回文件而不是文件夹

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

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

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

娜些时光,永不杰束 2024-10-07 17:29:02
  1. grep -r“你的字符串”*
    将在任何文件和文件夹中找到“yourstring”

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

  1. grep -rE "yourstring|yourotherstring|$" * 将搜索 yourstringyourotherstring 匹配的列表位置
  1. 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

  1. grep -rE "yourstring|yourotherstring|$" * will search for list locations where yourstring or yourotherstring matches
过去的过去 2024-10-07 17:29:02
grep -R "string" /directory/

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

grep -R "string" /directory/

-R follows also symlinks when -r does not.

猫烠⑼条掵仅有一顆心 2024-10-07 17:29:02

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

grep -nr yourString* .

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

因此,正确的方法是:

grep -nr \w*yourString\w* .

此命令在当前文件夹中搜索前后包含任何字符的字符串。

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.

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.

缺⑴份安定 2024-10-07 17:29:02

以下示例在位于文件夹 path1< 内某处的 *.xml*.js 文件中递归查找您的搜索字符串 /code>、path2path3

grep -r --include=*.xml --include=*.js "your search string" path1 path2 path3

因此,您可以在文件的子集中搜索许多目录,只需在末尾提供路径即可。

The following sample looks recursively for your search string in the *.xml and *.js files located somewhere inside the folders path1, path2 and path3.

grep -r --include=*.xml --include=*.js "your search string" path1 path2 path3

So you can search in a subset of the files for many directories, just providing the paths at the end.

谈下烟灰 2024-10-07 17:29:02

在目录内运行(终端)以下命令。它也会递归检查子目录内部。

grep -r 'your string goes here' * 

Run(terminal) the following command inside the directory. It will recursively check inside subdirectories too.

grep -r 'your string goes here' * 
又怨 2024-10-07 17:29:02
ls | grep -n <word>

将 ls 管道连接到 grep,这样您就可以在当前目录中搜索所需的任何内容。
-n 用于显示该行。

ls | grep -n <word>

Piping ls to grep, so you can search whatever you need inside the directory you currently are.
-n is used to show the line.

爱冒险 2024-10-07 17:29:02

不要使用 grep。下载 Silver Searcherripgrep。它们都很出色,而且比 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文