如何递归地grep所有目录和子目录?
find . | xargs grep "texthere" *
如何递归地grep所有目录和子目录?
find . | xargs grep "texthere" *
当前回答
另一种语法递归地grep Linux系统上所有文件中的字符串
grep-irn“字符串”
-r表示递归搜索,在给定目录和子目录中搜索指定字符串,在文件、程序等中查找指定字符串
-不区分大小写可用于添加大小写颠倒的字符串
-n打印指定字符串的行号
注意:这会将大量结果打印到控制台,所以您可能需要通过管道过滤输出,并删除不太有趣的信息,它还会搜索二进制程序,因此您可能需要过滤一些结果
其他回答
在POSIX系统中,找不到grep的-r参数和grep-rn“东西”。不会运行,但如果使用find命令,它将:
查找-键入f-exec grep-n“stuff”{}\-打印
Solaris和HP-UX同意。
只有文件名也很有用
grep -r -l "foo" .
2018年,您希望使用ripgrep或白银搜索器,因为它们比其他搜索器快得多。
下面是一个包含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
在OSX上,这将安装rigrep:brew install rigrep。这将安装silver搜索器:brew install the _silver_searcher。
如果您知道所需文件的扩展名或模式,另一种方法是使用--include选项:
grep -r --include "*.txt" texthere .
您还可以使用--exclude提及要排除的文件。
Ag
如果您经常搜索代码,Ag(银搜索器)是grep的一个更快的替代方案,它是为搜索代码而定制的。例如,默认情况下,它是递归的,并自动忽略.gitignore中列出的文件和目录,因此您不必一直向grep或find传递同样繁琐的排除选项。
grep-r“texthere”。(通知期结束)
(^信用:https://stackoverflow.com/a/1987928/1438029)
澄清:
grep-r“texthere”/(递归地grep所有目录和子目录)
grep-r“texthere”。(递归地grep这些目录和子目录)
grep递归
grep[选项]模式[文件…][选项]-R、 -R,--递归递归地读取每个目录下的所有文件。这相当于-d recurse或--directories=recurse选项。http://linuxcommand.org/man_pages/grep1.html
grep帮助
$grep--帮助
$ grep --help |grep recursive
-r, --recursive like --directories=recurse
-R, --dereference-recursive
选择
确认(http://beyondgrep.com/)
银(http://github.com/ggreer/the_silver_searcher)