239
votes

What is the option for grep that will allow me only to print the matching file and not the line within a file that matches the criteria?

4
I typically only use this command when searching for files that call a specific function or method.Gearoid Murphy
use case (search and replace foo->bar in all files recursively): sed -i 's/foo/bar/' `grep -lR 'foo'`user323094

4 Answers

374
votes
grep -l 

(That's a lowercase L)

48
votes

You can use the Unix-style -l switch – typically terse and cryptic – or the equivalent --files-with-matches – longer and more readable.

The output of grep --help is not easy to read, but it's there:

-l, --files-with-matches  print only names of FILEs containing matches
29
votes

-l (that's a lower-case L).

1
votes

Also remember one thing. Very important
You have to specify the command something like this to be more precise
grep -l "pattern" *