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?
Asked
Active
Viewed 1.3e+01k times
248
codeforester
- 34,080
- 14
- 96
- 122
paultop6
- 3,563
- 4
- 28
- 35
-
1I 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
-
6Possible 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 Answers
391
grep -l
(That's a lowercase L)
Vincent Scheib
- 15,430
- 9
- 58
- 75
a'r
- 34,325
- 7
- 64
- 65
-
4[Letter O Considered Harmful](https://en.wikipedia.org/wiki/Fortran#Humor) – user234461 Jan 23 '20 at 13:19
51
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
- 18,193
- 10
- 62
- 78
3
Also remember one thing. Very important
You have to specify the command something like this to be more precise
grep -l "pattern" *
Vinod Kumar
- 492
- 5
- 12