256

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?

codeforester
  • 39,467
  • 16
  • 112
  • 140
paultop6
  • 3,691
  • 4
  • 29
  • 37
  • 1
    I typically only use this command when searching for files that call a specific function or method. – Gearoid Murphy Sep 20 '12 at 08:26
  • use case (search and replace foo->bar in all files recursively): sed -i 's/foo/bar/' \`grep -lR 'foo'\` – user323094 Sep 08 '13 at 20:04
  • 6
    Possible duplicate of [How can I use grep to show just filenames (no in-line matches) on linux?](http://stackoverflow.com/questions/6637882/how-can-i-use-grep-to-show-just-filenames-no-in-line-matches-on-linux) – 7ochem Nov 30 '15 at 13:21

4 Answers4

403
grep -l 

(That's a lowercase L)

Vincent Scheib
  • 17,142
  • 9
  • 61
  • 77
a'r
  • 35,921
  • 7
  • 66
  • 67
52

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
Iain Samuel McLean Elder
  • 19,791
  • 12
  • 64
  • 80
30

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

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
4

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

Vinod Kumar
  • 562
  • 5
  • 12