开发者

use grep to return a list of files, given multiple keywords (like google returns a list of webpages)

开发者 https://www.devze.com 2023-01-17 15:13 出处:网络
I need to find ALL files that have multiple keywords anywhere in the file开发者_StackOverflow中文版 (not necessarily on the same line), given a starting directory like ~/. Does \"grep -ro\" do this?

I need to find ALL files that have multiple keywords anywhere in the file开发者_StackOverflow中文版 (not necessarily on the same line), given a starting directory like ~/. Does "grep -ro" do this?

(I'm using Unix, Mac OSX 10.4)


You can use the -l option to get a list of filenames with matches, so it's just a matter of finding all of the files that have the first keyword and then filtering that list down to the files that also have the second keyword:

grep -rl first_keyword basedir | xargs grep -l second_keyword


To search just *.txt

find ~/. -name "*.txt" | xargs grep -l first_keyword | xargs grep -l second_keyword

Thanks Adam!

0

精彩评论

暂无评论...
验证码 换一张
取 消