Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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
share|improve this question
add comment

2 Answers

up vote 7 down vote accepted
   -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 grep manpage

share|improve this answer
    
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 '11 at 16:23
    
Never mind; I tried -l instead and it worked. –  EmmyS Mar 25 '11 at 16:28
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.