1322

How can I use grep to show just file-names (no in-line matches) on Linux?

I am usually using something like:

find . -iname "*php" -exec grep -H myString {} \;

How can I just get the file-names (with paths), but without the matches? Do I have to use xargs? I didn't see a way to do this on my grep man page.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
cwd
  • 50,380
  • 51
  • 156
  • 196
  • 5
    You mean, you want the filenames that have at least one match? – leonbloy Jul 09 '11 at 22:27
  • 6
    Great question. I actually started using this to open a list of files returned by grep in Vim. `vim $(grep -rl Something app/)` – David Tuite Mar 20 '13 at 10:49
  • 2
    You cannot list files using grep command, it is basically used to fetch desired text from a file or a list. For instance, ps aux | grep 'apt-get' or grep 'text-to-find' /path/to/file/ – iamjayp Mar 17 '17 at 08:09
  • @iamjayp Ummm, you can too list files using the grep command. `grep -lr 'text-to-find' ./*` works quite nicely! – ErikE Nov 13 '21 at 21:58

4 Answers4

2031

The standard option grep -l (that is a lowercase L) could do this.

From the Unix standard:

-l
    (The letter ell.) Write only the names of files containing selected
    lines to standard output. Pathnames are written once per file searched.
    If the standard input is searched, a pathname of (standard input) will
    be written, in the POSIX locale. In other locales, standard input may be
    replaced by something more appropriate in those locales.

You also do not need -H in this case.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Random832
  • 35,197
  • 3
  • 42
  • 63
174

From the grep(1) man page:

  -l, --files-with-matches
          Suppress  normal  output;  instead  print the name of each input
          file from which output would normally have  been  printed.   The
          scanning  will  stop  on  the  first match.  (-l is specified by
          POSIX.)
Ignacio Vazquez-Abrams
  • 740,318
  • 145
  • 1,296
  • 1,325
62

For a simple file search, you could use grep's -l and -r options:

grep -rl "mystring"

All the search is done by grep. Of course, if you need to select files on some other parameter, find is the correct solution:

find . -iname "*.php" -execdir grep -l "mystring" {} +

The execdir option builds each grep command per each directory, and concatenates filenames into only one command (+).

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
0

My command suggestion for getting the filename with path

sudo find /home -name *.php

The output from this command on my Linux OS:

compose-sample-3/html/mail/contact_me.php

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
apl
  • 79
  • 1
  • 10
  • 1
    The question specifically asks how they get the file names _using grep_. – snakecharmerb Apr 11 '20 at 15:38
  • 2
    This command will actually not do what you expect. Shell expansion will first expand \*.php and then feed the result as argument 4 into find. You should always escape your file pattern with either \\*.php or "\*.php" when using find. – Spliffster Feb 24 '21 at 08:35