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 是个更好的选择。

How do I grep recursively?

  • wpiri asked:
    • How do I recursively grep all directories and subdirectories?
      • 我怎么用grep递归搜索所有目录与子目录?
    • find . | xargs grep "texthere" *
  • 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 ggrep command.
        • 注意:这只能用在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 --include option:
        • 如果你知道文件的扩展名或者模式,另一个方法是使用 --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的繁琐选项。
    • Kurt - vote: 133
      • Also:
        • 这样也行:
      • find ./ -type f -print0 | xargs -0 grep "foo"
      • but grep -r is a better answer.
        • 不过 grep -r 是个更好的选择。

版权声明:
作者:MWHLS
链接:http://panwj.top/2798.html
来源:无镣之涯
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
< <上一篇
下一篇>>