如何在LINUX中提取目录中的字符串及其所有子目录的文件?(复制)

时间:2022-10-06 19:19:28

This question already has an answer here:

这个问题已经有了答案:

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

如何在LINUX中对目录中的字符串或文本及其所有子目录文件进行grep ?

2 个解决方案

#1


341  

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' {} +

#2


39  

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 grep did, so in practice they (almost) all do. Some versions of grep support -R as well as (or conceivably instead of) -r; AFAICT, it means the same thing.

- r是递归;-e是可选的,但它的参数指定要搜索的regex。有趣的是,POSIX grep不需要支持-r(或-r),但是我几乎可以肯定System V grep会支持-r,所以实际上它们(几乎)都支持。一些版本的grep支持-R以及(或可以理解地代替)-R;它的意思是一样的。

#1


341  

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' {} +

#2


39  

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 grep did, so in practice they (almost) all do. Some versions of grep support -R as well as (or conceivably instead of) -r; AFAICT, it means the same thing.

- r是递归;-e是可选的,但它的参数指定要搜索的regex。有趣的是,POSIX grep不需要支持-r(或-r),但是我几乎可以肯定System V grep会支持-r,所以实际上它们(几乎)都支持。一些版本的grep支持-R以及(或可以理解地代替)-R;它的意思是一样的。