5

I'm trying to use grep to find a specific piece of text in a bunch of files on my web server. No problem, except that it returns way more information than I want! Ideally it would just return a list of files, and if the text exists in more than one place in the file it would only list the file name once.

Currently I'm using something like this:

grep -ir essay_ "mysite"

to do a case-insensitive recursive search for the word essay_ in all directories of my site. What it returns is something like this:

mysite/dot/contest_declaration.php:$questions = $db->get_results("SELECT * FROM dot_essay WHERE active = 1 order by display_order asc");
mysite/dot/contest_declaration.php:     $db->query("DELETE FROM dot_essay_xref WHERE entry_id = $svar_entry_id");
mysite/dot/contest_declaration.php:     $id = $question->essay_id;
mysite/dot/contest_declaration.php:     $answer = ${"essay_$id"};
shoppermarketexpo/dot/print_declaration.php:        $questions = $db->get_results("SELECT * FROM dot_essay WHERE active = 1");

What I'd like to get back is:

mysite/dot/contest_declaration.php
mysite/dot/print_declaration.php

2 Answers 2

10

-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.)

from the manual of the GNU implementation of grep. As noted above -l (l for list) is standard and portable, --files-with-matches (and long options in general) is a GNU extension.

2
  • I tried that, but must be putting it in the wrong place. grep -i-r --files-with-matches essay_ "shoppermarketexpo" grep: invalid option -- - Usage: grep [OPTION]... PATTERN [FILE]...
    – EmmyS
    Mar 25, 2011 at 16:23
  • Never mind; I tried -l instead and it worked.
    – EmmyS
    Mar 25, 2011 at 16:28
2

You want the -l (el) or --files-with-matches command line switch(es)

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.)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .