Stackoverflow热门问题(十九)-怎么用grep递归搜索?
stackoverflow热门问题目录如有翻译问题欢迎评论指出,谢谢。
怎么用grep递归搜索?
- wpiri asked:
- 我怎么用
grep递归搜索所有目录与子目录? find . | xargs grep "texthere" *
- 我怎么用
- Answers:
- Vinko Vrsalovic - vote: 2749
grep -r "texthere" .- 第一个参数是用于搜索的正则表达式,第二个则是表示搜索目录。这里的
.表示当前目录。 - 注意:这只能用在
GNU grep,在一些平台上,例如Solaris,必须使用GNU grep,而非传统实现。对于Solaris来说,则是ggrep。
- christangrant - vote: 744
- 如果你知道文件的扩展名或者模式,另一个方法是使用
--include选项: grep -r --include "*.txt" texthere .- 也能用
--exclude来排除。 - Ag:
- 如果你经常在代码里搜索,Ag (The Silver Searcher)是一个比grep更快的选择,它专门为代码搜索定制。例如,它默认递归,并且自动忽略在
.gitignore列出的文件及目录,所以如果你没必要用那些grep或者find的繁琐选项。
- 如果你知道文件的扩展名或者模式,另一个方法是使用
- Kurt - vote: 133
- 这样也行:
find ./ -type f -print0 | xargs -0 grep "foo"- 不过
grep -r是个更好的选择。
- Vinko Vrsalovic - vote: 2749
How do I grep recursively?
- wpiri asked:
- How do I recursively
grepall directories and subdirectories?- 我怎么用
grep递归搜索所有目录与子目录?
- 我怎么用
find . | xargs grep "texthere" *
- How do I recursively
- Answers:
- Vinko Vrsalovic - vote: 2749
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。
- 注意:这只能用在
- christangrant - vote: 744
- 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的繁琐选项。
- 如果你经常在代码里搜索,Ag (The Silver Searcher)是一个比grep更快的选择,它专门为代码搜索定制。例如,它默认递归,并且自动忽略在
- If you know the extension or pattern of the file you would like, another method is to use
- Kurt - vote: 133
- Also:
- 这样也行:
find ./ -type f -print0 | xargs -0 grep "foo"- but
grep -ris a better answer.- 不过
grep -r是个更好的选择。
- 不过
- Also:
- Vinko Vrsalovic - vote: 2749


共有 0 条评论