Andy
Andy

Reputation: 2830

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 please explain why the following does not work?

grep -ilrw "hot" config*

Upvotes: 2

Views: 9576

Answers (3)

AlG
AlG

Reputation: 15157

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.

Upvotes: 4

Maxim Egorushkin
Maxim Egorushkin

Reputation: 136208

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

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

Upvotes: 0

johusman
johusman

Reputation: 3472

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

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

Upvotes: 4

Related Questions