How do I grep recursively?

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

find . | xargs grep "texthere" *

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 ggrep command.


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

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

You can also mention files to exclude with --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.


Also:

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

but grep -r is a better answer.

链接地址: http://www.djcxy.com/p/3312.html

上一篇: 如何在git历史中grep(搜索)提交的代码?

下一篇: 如何递归地grep?