Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 691
AndyL Avatar asked Aug 21 '10 21:08

AndyL


People also ask

How will you find files recursively that contains specific words in their filename?

You can use grep tool to search recursively the current folder, like: grep -r "class foo" . Alternatively, use ripgrep .

How do I search for a file recursively?

Using Find You can also use the find command followed by the target directory and the file you wish to locate. The command will start in the root directory and recursively search all the subdirectories and locate any file with the specified name.

Which command will search recursively for the word in all files in a directory tree?

You can use grep command or find command as follows to search all files for a string or words recursively.

How do I search for a directory in recursively?

An easy way to do this is to use find | egrep string . If there are too many hits, then use the -type d flag for find. Run the command at the start of the directory tree you want to search, or you will have to supply the directory as an argument to find as well. Another way to do this is to use ls -laR | egrep ^d .


1 Answers

try this:

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

note: not actually tested under msys.

like image 196
Lesmana Avatar answered Sep 21 '22 09:09

Lesmana