开发者

UNIX grep - how to recusively find text in specifically-named files?

I would like a list of the filenames of files containing the word "hot" (don't care about case). The files should also be under the directory /test/home, with filenames starting with "config".

  1. So far the best I can come up with is the following. However, it only works in the directory /test/home.

grep -ilrw "hot" * | grep -i config

Another disadvantage of this approach is that it is not possible to check for filenames starting with "config". For example, "grep -i ^config" will not match files such as "/test/home/configurations/config.xml"

Is there a way to work round this?

  1. Could somebody pleas开发者_StackOverflow中文版e explain why the following does not work?

grep -ilrw "hot" config*


find . -name 'config*' | xargs grep -i "hot"

may depend a bit on your flavor of UNIX, I think


If you are looking to use only grep you weren't far off. I'd do;

grep -Rli --include='config*' "hot" *

--include, allows you to specify a GLOB to search and the final * allows you to tweak the files/directories that the include glob will work on.


If you are a lucky user of GNU powertools, find can do that for you:

find /test/home -iregex ".*hot.*" -regex "config.*"
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜