wpi*_*iri 1619 unix linux grep
我如何递归grep
所有目录和子目录?
find . | xargs grep "texthere" *
Run Code Online (Sandbox Code Playgroud)
Vin*_*vic 2430
grep -r "texthere" .
Run Code Online (Sandbox Code Playgroud)
第一个参数表示要搜索的正则表达式,而第二个参数表示应搜索的目录.在这种情况下,.
表示当前目录.
注意:这适用于GNU grep,在某些平台(如Solaris)上,您必须专门使用GNU grep而不是遗留实现.对于Solaris,这是ggrep
命令.
chr*_*ant 655
如果你知道你想要的文件的扩展名或模式,另一种方法是使用--include
选项:
grep -r --include "*.txt" texthere .
Run Code Online (Sandbox Code Playgroud)
您还可以提及要排除的文件--exclude
.
如果你经常搜索代码,Ag(The Silver Searcher)是一个比grep更快的替代品,它是为搜索代码而定制的.例如,它默认是递归的,并自动忽略列出的文件和目录.gitignore
,因此您不必继续将相同的繁琐排除选项传递给grep或find.
Kur*_*urt 124
也:
find ./ -type f -print0 | xargs -0 grep "foo"
Run Code Online (Sandbox Code Playgroud)
但是grep -r
更好的答案.
Von*_*onC 111
我现在总是使用(甚至在Windows上使用GoW - Gnu在Windows上):
grep --include="*.xxx" -nRHI "my Text to grep" *
Run Code Online (Sandbox Code Playgroud)
这包括以下选项:
--include=PATTERN
Run Code Online (Sandbox Code Playgroud)
递归仅搜索文件匹配的目录
PATTERN
.
-n, --line-number
Run Code Online (Sandbox Code Playgroud)
使用输入文件中的行号为每行输出添加前缀.
(注意:phuclv 在评论中 添加-n
了很多性能,因此您可能希望跳过该选项)
-R, -r, --recursive
Run Code Online (Sandbox Code Playgroud)
递归地读取每个目录下的所有文件; 这相当于
-d recurse
选项.
-H, --with-filename
Run Code Online (Sandbox Code Playgroud)
打印每个匹配的文件名.
-I
Run Code Online (Sandbox Code Playgroud)
处理二进制文件,就好像它不包含匹配数据一样;
这相当于--binary-files=without-match
选项.
如果我想要不区分大小写的结果,我可以添加' i
'(-nRHIi
).
我可以得到:
/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;
...
Run Code Online (Sandbox Code Playgroud)
roo*_*ook 22
在POSIX系统中,您没有找到-r
参数,grep
并且您grep -rn "stuff" .
将无法运行,但如果使用find
命令,它将:
find . -type f -exec grep -n "stuff" {} \; -print
同意Solaris
和HP-UX
.
ken*_*orb 19
**
使用grep -r
作品,但它可能过度,特别是在大文件夹中.
有关更实际的用法,以下是使用globbing语法(**
)的语法:
grep "texthere" **/*.txt
Run Code Online (Sandbox Code Playgroud)
仅使用模式选择模式对特定文件进行grepping.它适用于支持的shell,如Bash +4或zsh.
要激活此功能,请运行:shopt -s globstar
.
git grep
对于Git版本控制下的项目,请使用:
git grep "pattern"
Run Code Online (Sandbox Code Playgroud)
哪个更快.
ripgrep
对于较大的项目,最快的ripgrep
grepping 工具是默认情况下递归greps文件:
rg "pattern" .
Run Code Online (Sandbox Code Playgroud)
它建立在Rust的正则表达式引擎之上,它使用有限自动机,SIMD和积极的文字优化来快速搜索.在这里查看详细分析.
gee*_*eek 15
另一种在 Linux 系统上的所有文件中递归地 grep 字符串的语法
grep -irn "string"
Run Code Online (Sandbox Code Playgroud)
命令的细分
-r, --recursive
Run Code Online (Sandbox Code Playgroud)
表示
recursive
在给定目录和子目录中查找指定字符串的搜索,在文件、二进制文件等中查找特定字符串
-i, --ignore-case
Run Code Online (Sandbox Code Playgroud)
忽略大小写,可用于添加大小写颠倒的字符串
-n, --line-number
Run Code Online (Sandbox Code Playgroud)
打印找到的文件中指定字符串的行号
注意:这会将大量结果打印到控制台,因此您可能需要通过管道过滤输出并删除不太有趣的信息。它还搜索二进制程序,因此您可能想要过滤一些结果
Gir*_*ore 11
要查找files
以path
递归方式包含string
以下命令的特定用途的名称UNIX
:
find . | xargs grep "searched-string"
Run Code Online (Sandbox Code Playgroud)
用于Linux
:
grep -r "searched-string" .
Run Code Online (Sandbox Code Playgroud)
在UNIX
服务器上找到一个文件
find . -type f -name file_name
Run Code Online (Sandbox Code Playgroud)
在LINUX服务器上找到一个文件
find . -name file_name
Run Code Online (Sandbox Code Playgroud)
如果您只想关注实际目录,而不是符号链接,
grep -r "thingToBeFound" directory
Run Code Online (Sandbox Code Playgroud)
如果你想跟随符号链接和实际目录(注意无限递归),
grep -R "thing to be found" directory
Run Code Online (Sandbox Code Playgroud)
由于您尝试递归grep,以下选项也可能对您有用:
-H: outputs the filename with the line
-n: outputs the line number in the file
Run Code Online (Sandbox Code Playgroud)
因此,如果要在当前目录或任何子目录中查找包含Darth Vader的所有文件并捕获文件名和行号,但不希望递归遵循符号链接,则命令将为
grep -rnH "Darth Vader" .
Run Code Online (Sandbox Code Playgroud)
如果你想在目录中找到所有提到的单词cat
/home/adam/Desktop/TomAndJerry
Run Code Online (Sandbox Code Playgroud)
而你目前在目录中
/home/adam/Desktop/WorldDominationPlot
Run Code Online (Sandbox Code Playgroud)
并且您想要捕获文件名但不是字符串"cats"的任何实例的行号,并且您希望递归遵循符号链接,如果找到它们,您可以运行以下任一项
grep -RH "cats" ../TomAndJerry #relative directory
grep -RH "cats" /home/adam/Desktop/TomAndJerry #absolute directory
Run Code Online (Sandbox Code Playgroud)
资源:
运行"grep --help"
关于符号链接的简短介绍,对于任何阅读这个答案并且被我对它们的引用感到困惑的人:https: //www.nixtutor.com/freebsd/understanding-symbolic-links/
ag是我最喜欢的方式,现在github.com/ggreer/the_silver_searcher.它与ack基本相同,但还有一些优化.
这是一个简短的基准.我在每次测试之前清除缓存(参见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
Run Code Online (Sandbox Code Playgroud)
如果您要从目录结构中查找所有文件中的特定内容,您可以使用,find
因为您更清楚自己在做什么:
find -type f -exec grep -l "texthere" {} +
Run Code Online (Sandbox Code Playgroud)
请注意-l
(L的小写)显示包含文本的文件的名称.如果您想要打印匹配本身,请将其删除.或者用于-H
将文件与匹配一起获取.总之,其他替代方案是:
find -type f -exec grep -Hn "texthere" {} +
Run Code Online (Sandbox Code Playgroud)
在哪里-n
打印行号.
grep -r "texthere" .
(通知期见文末)
(^信用:/sf/answers/139154991/)
澄清:
grep -r "texthere" /
(递归地 grep所有目录和子目录)
grep -r "texthere" .
(递归地grep这些目录和子目录)
grep [options] PATTERN [FILE...]
[选项]
-R, -r, --recursive
递归读取每个目录下的所有文件。
这相当于
-d recurse
or--directories=recurse
选项。
$ grep --help
$ grep --help |grep recursive
-r, --recursive like --directories=recurse
-R, --dereference-recursive
Run Code Online (Sandbox Code Playgroud)
ag
(http://github.com/ggreer/the_silver_searcher)
这是我当前机器上的情况(Windows 7上的git bash):
find ./ -type f -iname "*.cs" -print0 | xargs -0 grep "content pattern"
Run Code Online (Sandbox Code Playgroud)
对于带空格的路径,我总是忘记-print0和-0.
编辑:我现在的首选工具是ripgrep:https://github.com/BurntSushi/ripgrep/releases.它真的很快并且具有更好的默认值(默认情况下是递归的).与我原来的答案相同,但使用ripgrep:rg -g "*.cs" "content pattern"
把我的两分钱扔在这里。正如其他人已经提到的那样,grep -r并不适用于每个平台。这可能听起来很傻,但我总是使用 git。
git grep "texthere"
Run Code Online (Sandbox Code Playgroud)
即使目录没有暂存,我也只是暂存它并使用 git grep。
String
以下是递归搜索Unix
环境的命令Linux
。
命令UNIX
是:
find . -name "string to be searched" -exec grep "text" "{}" \;
Run Code Online (Sandbox Code Playgroud)
命令Linux
是:
grep -r "string to be searched" .
Run Code Online (Sandbox Code Playgroud)
2018 年,您想要使用ripgrep
或 ,the-silver-searcher
因为它们比其他替代方案快得多。
这是一个包含 336 个一级子目录的目录:
% find . -maxdepth 1 -type d | wc -l
336
% time rg -w aggs -g '*.py'
...
rg -w aggs -g '*.py' 1.24s user 2.23s system 283% cpu 1.222 total
% time ag -w aggs -G '.*py$'
...
ag -w aggs -G '.*py$' 2.71s user 1.55s system 116% cpu 3.651 total
% time find ./ -type f -name '*.py' | xargs grep -w aggs
...
find ./ -type f -name '*.py' 1.34s user 5.68s system 32% cpu 21.329 total
xargs grep -w aggs 6.65s user 0.49s system 32% cpu 22.164 total
Run Code Online (Sandbox Code Playgroud)
在 OSX 上,这会安装ripgrep
:brew install ripgrep
。这将安装silver-searcher
:brew install the_silver_searcher
.