I have around 500 strings and I want to search for the files which contain them inside a directory and get the file names which contain the strings. So far I've been using:

find -name 'LYFNRE.*' -exec grep -f file1.txt {} \; -print

but the problem is a string can be found in many files so it is difficult to find which strings are present and which are missing due to the huge output. Can you help me in printing the strings with their corresponding file names where they were found.

It's not using egrep that makes a difference, it's passing the whole list of file names on the command line (and you don't explain how to do that).
–
GillesJun 18 '14 at 22:14

well that depends on the where the files are located. If there is a constant list of files, you can use: "for f in ${file_list[@]} ; do echo $f ; egrep -n str1|str2|str3 $f ; done" . if all files are in same directory: "egrep -rn "str1|str2" dir_name" . if there is a constant list of directories that include all the files, use: "for dir in ${dir_list[@]} ; do egrep -rn "str1|str2" dir_name ; done" . read man egrep for input/out specifications, but it is pretty similar to grep
–
csnyJun 19 '14 at 6:54

The problem is that you're passing one file at a time to grep. When grep sees a single file on its command line, it assumes that you know exactly where you'r searching, so it doesn't display the file name in front of matches.

A trick to force grep to always output a file name is to also pass /dev/null (where there won't be any match ever). Some grep implementations have an option for that: -H.

Additionally, you can use -exec … {} + instead of -exec … {} \; to execute the program for many files in one go. This is faster. You should still pass /dev/null or -H, because it could happen that the command is invoked on exactly one file, either because there is a single matching file or because there are many matches requiring that grep is invoked several times and one time happens to concern a single file.

Alternatively, you can do recursive globbing in your shell. In zsh, this works out of the box. In bash, you need to run shopt -s globstar first, and beware that bash recurses through symbolic links to directories (unlike find or zsh).