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

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

find . | xargs grep "texthere" *
2006843 次浏览
grep -r "texthere" .

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

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

还有:

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

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

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

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

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

Ag

如果你经常搜索代码,银搜索者(The Silver Searcher)是grep的更快替代品,它是为搜索代码而定制的。例如,它默认是递归的,并自动忽略.gitignore中列出的文件和目录,所以你不必一直向grep或查找传递同样繁琐的排除选项。

只是文件名也可以很有用

grep -r -l "foo" .

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

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

(正如的评论中的克朗所指出的,您可以添加2>/dev/null来取消权限拒绝输出)

这包括以下选项:

--include=PATTERN

仅在目录中递归搜索与PATTERN匹配的文件。

-n, --line-number

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

(注意:phuclv在评论添加到#0大大降低了性能,因此您可能希望跳过该选项)

-R, -r, --recursive

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

-H, --with-filename

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

-I

处理二进制文件,就好像它不包含匹配的数据一样;
这相当于--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 listvendor/klaussilveira/gitter/lib/Gitter/Client.php:176:        return $this->hidden;...

在POSIX系统中,您找不到grep-r参数,您的grep -rn "stuff" .也不会运行,但如果您使用find命令,它将:

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

同意SolarisHP-UX

这应该工作:

grep -R "texthere" *

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

最好的选择是grep -r,但如果不可用,请使用find . -type f -exec grep -H whatever {} \;

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

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

ryan@3G08$ sync && echo 3 | sudo tee /proc/sys/vm/drop_caches3ryan@3G08$ time grep -r "hey ya" .
real    0m9.458suser    0m0.368ssys 0m3.788sryan@3G08:$ sync && echo 3 | sudo tee /proc/sys/vm/drop_caches3ryan@3G08$ time ack-grep "hey ya" .
real    0m6.296suser    0m0.716ssys 0m1.056sryan@3G08$ sync && echo 3 | sudo tee /proc/sys/vm/drop_caches3ryan@3G08$ time ag "hey ya" .
real    0m5.641suser    0m0.356ssys 0m3.444sryan@3G08$ time ag "hey ya" . #test without first clearing cache
real    0m0.154suser    0m0.224ssys 0m0.172s

在我的IBMAIX Server(OS版本:AIX 5.2)中,使用:

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

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

./inc/xxxx_x. h

2865:/**描述: stringYouWannaSearch*/

不管怎样,它对我有用:)

只是为了好玩,快速而肮脏地搜索*. txt文件,如果@christang犯人的答案太多而无法输入:-)

grep -r texthere .|grep .txt

如果您正在从目录结构的所有文件中查找特定内容,您可以使用find,因为它更清楚您在做什么:

find -type f -exec grep -l "texthere" {} +

请注意,-l(L的下大写)显示包含文本的文件名。如果您想打印匹配本身,请删除它。或者使用-H将文件与匹配一起获取。总之,其他替代方案是:

find -type f -exec grep -Hn "texthere" {} +

其中-n打印行号。

要查找files的名称,其中path递归包含特定的string,请使用以下命令UNIX

find . | xargs grep "searched-string"

对于Linux

grep -r "searched-string" .

UNIX服务器上查找文件

find . -type f -name file_name

在LINUX服务器上查找文件

find . -name file_name

grep -r "texthere" .(通知期结束时)

(图片来源:https://stackoverflow.com/a/1987928/1438029


澄清:

grep -r "texthere" /(递归grep所有目录和子目录)

grep -r "texthere" .(递归grep这些目录和子目录)

grep递归

grep [options] PATTERN [FILE...]

[选项]

-R, -r, --recursive

递归读取每个目录下的所有文件。

这相当于-d recurse--directories=recurse选项。

http://linuxcommand.org/man_pages/grep1.html

grep帮助

$ grep --help

$ grep --help |grep recursive-r, --recursive           like --directories=recurse-R, --dereference-recursive

替代品

ackhttp://beyondgrep.com/

aghttp://github.com/ggreer/the_silver_searcher

下面是在UnixLinux环境中递归搜索String的命令。

UNIX命令是:

find . -name "string to be searched" -exec grep "text" "{}" \;

Linux命令是:

grep -r "string to be searched" .

这是在我当前机器上为我的案例工作的那个(Windows 7上的git bash):

find ./ -type f -iname "*.cs" -print0 | xargs -0 grep "content pattern"

对于带空格的路径,我总是忘记-print0和-0。

编辑:我现在首选的工具是ripgrep:https://github.com/BurntSushi/ripgrep/releases。它真的很快,并且有更好的默认值(比如默认递归)。与我最初的答案相同的例子,但使用了ripgrep:rg -g "*.cs" "content pattern"

如果您只想遵循实际目录,而不是符号链接,

grep -r "thingToBeFound" directory

如果你想跟踪符号链接和实际目录(小心无限递归),

grep -R "thing to be found" directory

由于您正在尝试递归grep,因此以下选项可能对您有用:

-H: outputs the filename with the line
-n: outputs the line number in the file

因此,如果您想在当前目录或任何子目录中查找包含Darth Vader的所有文件并捕获文件名和行号,但不希望递归跟随符号链接,则命令将是

grep -rnH "Darth Vader" .

如果你想在目录中找到所有提到猫这个词的地方

/home/adam/Desktop/TomAndJerry

你现在在目录里

/home/adam/Desktop/WorldDominationPlot

如果您想捕获文件名而不是字符串“cat”的任何实例的行号,并且您希望递归在找到符号链接时跟随符号链接,您可以运行以下任一操作

grep -RH "cats" ../TomAndJerry                   #relative directory
grep -RH "cats" /home/adam/Desktop/TomAndJerry   #absolute directory

来源:

运行“grep--help”

符号链接的简短介绍,对于任何阅读此答案并对我对它们的引用感到困惑的人:https://www.nixtutor.com/freebsd/understanding-symbolic-links/

我猜这就是你想写的

grep myText $(find .)

如果您想找到grep命中的文件,这可能会有其他帮助

grep myText $(find .) | cut -d : -f 1 | sort | uniq

这是一个递归(使用bash和sh进行了轻度测试)函数,它遍历给定文件夹(1美元)的所有子文件夹,并在给定文件(2美元)中使用grep搜索给定字符串(3美元):

$ cat script.sh#!/bin/sh
cd "$1"
loop () {for i in *doif [ -d "$i" ]then# echo entering "$i"cd "$i"loop "$1" "$2"fidone
if [ -f "$1" ]thengrep -l "$2" "$PWD/$1"fi
cd ..}
loop "$2" "$3"

运行它和一个示例输出:

$ sh script start_folder filename search_string/home/james/start_folder/dir2/filename

有关可用标志的列表:

grep --help

返回当前目录中regexp文本的所有匹配项,并使用相应的行号:

grep -rn "texthere" .

返回文本的所有匹配项,从根目录开始,具有相应的行号并忽略大小写:

grep -rni "texthere" /

此处使用的标志:

  • -r递归
  • -n带输出的打印行号
  • -i忽略大小写

在2018年,您希望使用ripgrepthe-silver-searcher,因为它们比替代方案快得多。

这是一个包含336个一级子目录的目录:

% find . -maxdepth 1 -type d | wc -l336
% 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 totalxargs grep -w aggs  6.65s user 0.49s system 32% cpu 22.164 total

在OSX上,这安装ripgrepbrew install ripgrep。这安装silver-searcherbrew install the_silver_searcher

全局查询**

使用grep -r可以工作,但它可能会过度,特别是在大文件夹中。

对于更实际的用法,这里是使用全局语法**)的语法:

grep "texthere" **/*.txt

它只使用模式选择模式的特定文件。它适用于支持的shell,例如bash+4zsh

要激活此功能,请运行:shopt -s globstar

另见:如何在Linux上找到包含特定文本的所有文件?

git grep

对于Git版本控制下的项目,请使用:

git grep "pattern"

这要快得多。

ripgrep

对于较大的项目,最快的greping工具是#0,默认情况下递归greps文件:

rg "pattern" .

它建立在Rust的正则表达式引擎之上,它使用有限自动机、SIMD和积极的文字优化来使搜索速度非常快。检查详细分析在这里

把我的两分钱扔在这里。正如其他人已经提到的grep-r并不适用于每个平台。这可能听起来很愚蠢,但我总是使用git。

git grep "texthere"

即使目录没有暂存,我也只是暂存它并使用git grep。

对于. gz文件,递归扫描所有文件和目录更改文件类型或放置*

find . -name \*.gz -print0 | xargs -0 zgrep "STRING"

另一种递归地在Linux系统的所有文件中grep字符串的语法

grep -irn "string"

-r表示recursive搜索,在给定目录和子目录中搜索指定的字符串,在文件、程序等中查找指定的字符串

-i ingnore区分大小写可用于添加倒排大小写字符串

-n打印指定字符串的行号

注意:这会将大量结果打印到控制台,因此您可能需要通过管道过滤输出并删除不太有趣的信息,它还会搜索二进制程序,因此您可能想要过滤一些结果