开发者

How to grep directory for keyword existence?

开发者 https://www.devze.com 2023-01-22 01:06 出处:网络
For example I ha开发者_运维技巧ve a /path/to/folder and want to see if it contains \"keyword1\", \"keyword2\" or \"keyword3\" and the result would be (when 2 are found):

For example I ha开发者_运维技巧ve a /path/to/folder and want to see if it contains "keyword1", "keyword2" or "keyword3" and the result would be (when 2 are found):

/path/to/folder: keyword1 keyword3

I tried with options shown here but it doesn't work for folders.


echo -n '/path/to/folder:'; for kw in {keyword1,keyword2,keyword3}; do grep -qr $kw /path/to/folder/; if [ $? == 0 ]; then echo -n " "$kw; fi; done


If you are talking about keywords in filenames

shopt -s nullglob
for file in *keyword1* *keyword2* *keyword3*
do
  echo "$file"
done

if you are talking about finding those keywords in the files that are in your folder, you can use tools like grep

grep -l -E "keyword1|keyword2|keyword3" *

if you need to show which keywords are found

grep  -Eo "keyword1|keyword3|keyword2" *


grep -E "keyword1|keyword3|keyword2" *

will return something like this:

file.one:This text contains keyword1.
file.two:The keyword2 can be found in this file.
file.six:This is a keyword enumeration: keyword1, keyword2, keyword3.

0

精彩评论

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

关注公众号