如何在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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 22:33:25  来源:igfitidea点击:

How to grep a string in a directory and all its subdirectories' files in LINUX?

linuxunixgrepcentos

提问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

-ris for recursive; -eis optional but its argument specifies the regex to search for. Interestingly, POSIX grepis not required to support -r(or -R), but I'm practically certain that System V grepdid, soin practice they (almost) all do. Some versions of grepsupport -Ras 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,这意味着同样的事情。