Linux 我如何递归地grep?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1987926/
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-08-03 19:38:19  来源:igfitidea点击:

How do I grep recursively?

linuxunixgrep

提问by wpiri

How do I recursively grepall directories and subdirectories?

如何递归grep所有目录和子目录?

find . | xargs grep "texthere" *

采纳答案by Vinko Vrsalovic

grep -r "texthere" .

The first parameter represents the regular expression to search for, while the second one represents the directory that should be searched. In this case, .means the current directory.

第一个参数表示要搜索的正则表达式,而第二个参数表示应该搜索的目录。在这种情况下,.表示当前目录。

Note: This works for GNU grep, and on some platforms like Solaris you must specifically use GNU grep as opposed to legacy implementation. For Solaris this is the ggrepcommand.

注意:这适用于 GNU grep,并且在某些平台(如 Solaris)上,您必须专门使用 GNU grep,而不是传统实现。对于 Solaris,这是ggrep命令。

回答by Kurt

Also:

还:

find ./ -type f -print0 | xargs -0 grep "foo"

but grep -ris a better answer.

但是grep -r一个更好的答案。

回答by christangrant

If you know the extension or pattern of the file you would like, another method is to use --includeoption:

如果您知道所需文件的扩展名或模式,另一种方法是使用--include选项:

grep -r --include "*.txt" texthere .

You can also mention files to exclude with --exclude.

您还可以提及要排除的文件--exclude

Ag

If you frequently search through code, Ag (The Silver Searcher)is a much faster alternative to grep, that's customized for searching code. For instance, it's recursive by default and automatically ignores files and directories listed in .gitignore, so you don't have to keep passing the same cumbersome exclude options to grep or find.

如果您经常搜索代码,Ag(The Silver Searcher)是 grep 的更快替代品,它是为搜索代码而定制的。例如,默认情况下它是递归的,并自动忽略 中列出的文件和目录.gitignore,因此您不必一直将相同的繁琐排除选项传递给 grep 或 find。

回答by chim

just the filenames can be useful too

只是文件名也很有用

grep -r -l "foo" .

回答by VonC

I now always use (even on Windows with GoW -- Gnu on Windows):

我现在总是使用(即使在带有GoW 的Windows 上-Windows 上的Gnu):

grep --include="*.xxx" -nRHI "my Text to grep" *

That includes the following options:

这包括以下选项:

--include=PATTERN

Recurse in directories only searching file matching PATTERN.

在目录中递归只搜索匹配的文件PATTERN

-n, --line-number

Prefix each line of output with the line number within its input file.

使用输入文件中的行号为每一行输出添加前缀。

(Note: phuclvadds in the commentsthat -ndecreases performance a lot so, so you might want to skip that option)

(注:phuclv在评论-n会降低性能了很多,所以,所以你可能想跳过该选项)

-R, -r, --recursive

Read all files under each directory, recursively; this is equivalent to the -d recurseoption.

递归读取每个目录下的所有文件;这相当于-d recurse选项。

-H, --with-filename

Print the filename for each match.

打印每个匹配项的文件名。

-I     

Process a binary file as if it did not contain matching data;
this is equivalent to the --binary-files=without-matchoption.

处理一个二进制文件,就好像它不包含匹配的数据一样;
这相当于--binary-files=without-match选项。

And I can add 'i' (-nRHIi), if I want case-insensitive results.

如果我想要不区分大小写的结果,我可以添加 ' i' ( -nRHIi)。

I can get:

我可以得到:

/home/vonc/gitpoc/passenger/gitlist/github #grep --include="*.php" -nRHI "hidden" *
src/GitList/Application.php:43:            'git.hidden'      => $config->get('git', 'hidden') ? $config->get('git', 'hidden') : array(),
src/GitList/Provider/GitServiceProvider.php:21:            $options['hidden'] = $app['git.hidden'];
tests/InterfaceTest.php:32:        $options['hidden'] = array(self::$tmpdir . '/hiddenrepo');
vendor/klaussilveira/gitter/lib/Gitter/Client.php:20:    protected $hidden;
vendor/klaussilveira/gitter/lib/Gitter/Client.php:170:     * Get hidden repository list
vendor/klaussilveira/gitter/lib/Gitter/Client.php:176:        return $this->hidden;
...

回答by rook

In POSIX systems, you don't find -rparameter for grepand your grep -rn "stuff" .won't run, but if you use findcommand it will:

在POSIX系统中,你找不到-r的参数grep和你grep -rn "stuff" .将无法运行,但如果你使用find命令将:

find . -type f -exec grep -n "stuff" {} \; -print

find . -type f -exec grep -n "stuff" {} \; -print

Agreed by Solarisand HP-UX.

Solaris和同意HP-UX

回答by sumit kumar

This should work:

这应该有效:

grep -R "texthere" *

回答by m.thome

Note that find . -type f | xargs grep whateversorts of solutions will run into "Argument list to long" errors when there are too many files matched by find.

请注意,find . -type f | xargs grep whatever当 find 匹配的文件太多时,各种解决方案将遇到“参数列表长”错误。

The best bet is grep -rbut if that isn't available, use find . -type f -exec grep -H whatever {} \;instead.

最好的办法是,grep -r但如果那不可用,请find . -type f -exec grep -H whatever {} \;改用。

回答by dranxo

ag is my favorite way to do this now github.com/ggreer/the_silver_searcher. It's basically the same thing as ack but with a few more optimizations.

ag 是我现在最喜欢的方式github.com/ggreer/the_silver_searcher。它与 ack 基本相同,但还有一些优化。

Here's a short benchmark. I clear the cache before each test (cf https://askubuntu.com/questions/155768/how-do-i-clean-or-disable-the-memory-cache)

这是一个简短的基准。我在每次测试之前清除缓存(参见https://askubuntu.com/questions/155768/how-do-i-clean-or-disable-the-memory-cache

ryan@3G08$ sync && echo 3 | sudo tee /proc/sys/vm/drop_caches
3
ryan@3G08$ time grep -r "hey ya" .

real    0m9.458s
user    0m0.368s
sys 0m3.788s
ryan@3G08:$ sync && echo 3 | sudo tee /proc/sys/vm/drop_caches
3
ryan@3G08$ time ack-grep "hey ya" .

real    0m6.296s
user    0m0.716s
sys 0m1.056s
ryan@3G08$ sync && echo 3 | sudo tee /proc/sys/vm/drop_caches
3
ryan@3G08$ time ag "hey ya" .

real    0m5.641s
user    0m0.356s
sys 0m3.444s
ryan@3G08$ time ag "hey ya" . #test without first clearing cache

real    0m0.154s
user    0m0.224s
sys 0m0.172s

回答by user3606336

In my IBM AIX Server (OS version: AIX 5.2), use:

在我的 IBM AIX 服务器(操作系统版本:AIX 5.2)中,使用:

find ./ -type f -print -exec grep -n -i "stringYouWannaFind" {} \; 

this will print out path/file name and relative line number in the file like:

这将打印出文件中的路径/文件名和相对行号,例如:

./inc/xxxx_x.h

./inc/xxxx_x.h

2865: /** Description : stringYouWannaFind */

2865:/** 描述:stringYouWannaFind */

anyway,it works for me : )

无论如何,它对我有用:)