如何在LINUX中grep目录及其所有子目录的文件中的字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15622328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to grep a string in a directory and all its subdirectories' files in LINUX?
提问by Rogers
How to grep a string or a text in a directory and all its subdirectories'files in LINUX ??
如何在LINUX中grep目录及其所有子目录的文件中的字符串或文本?
采纳答案by John Kugelman
If your grep supports -R
, do:
如果您的 grep 支持-R
,请执行以下操作:
grep -R 'string' dir/
If not, then use find
:
如果没有,则使用find
:
find dir/ -type f -exec grep -H 'string' {} +
回答by Jonathan Leffler
grep -r -e string directory
-r
is for recursive; -e
is optional but its argument specifies the regex to search for. Interestingly, POSIX grep
is not required to support -r
(or -R
), but I'm practically certain that System V in practice they (almost) all do. Some versions of grep
did, sogrep
support -R
as well as (or conceivably instead of) -r
; AFAICT, it means the same thing.
-r
用于递归;-e
是可选的,但它的参数指定要搜索的正则表达式。有趣的是,POSIXgrep
不需要支持-r
(或-R
),但我几乎可以肯定 System V 支持实际上它们(几乎)都支持。某些版本的grep
,所以grep
支持-R
以及(或可以想象代替)-r
;AFAICT,这意味着同样的事情。