Recursively search for files of a given name, and find instances of a particular phrase AND display the path to that file

AndyL picture AndyL · Aug 21, 2010 · Viewed 25.3k times · Source

I have a bunch of folders and subfolders. Each one contains, amongst other things, a text file called index.yml with useful data. I want to search through all of the different index.yml files to find instances of a search string. I must be able to see a few lines of context and the directory of the index.yml file that was found.

This almost works, but it doesn't give me the filename:

cat `find . -name 'index.yml'`| grep -i -C4 mySearchString

How can I do this and get the filename?

I am stuck on Windows with using msys. Note I don't seem to have full GNU grep, so I can't run grep --exclude or grep -R as suggested in other SO questions.

Answer

lesmana picture lesmana · Aug 21, 2010

try this:

find -name "index.yml" -exec grep -i -H -C4 pattern {} \;

note: not actually tested under msys.