List Files Containing Searched-for Words


-l

The grep command lists the lines containing the term for which you searched, but there might be times when you don't want to know the lines; instead, you want to know the names of the files that contain those matched lines. Previously in "Search for Text in Files, Ignoring Case," you looked for lines in H.P. Lovecraft stories that contained the word hideous. With the -l (or --files-with-matches) option, you can instead produce a list of those files (the -i is for case-insensitive searches, remember).

$ grep -il hideous h_p_lovecraft/* h_p_lovecraft/Call of Cthulhu.txt h_p_lovecraft/From Beyond.txt h_p_lovecraft/The Case of Charles Dexter Ward.txt 


This type of result is particularly useful when combined with other commands. For example, if you wanted to print a list of Lovecraft's stories containing the word hideous, you could combine grep with the lpr command as follows:

$ grep -il hideous h_p_lovecraft/* | lpr 


Keep in mind that this command would print out the list of stories, not the stories themselves (there is a way to do that, though, and here's a hint: It involves cat).



Linux Phrasebook
Linux Phrasebook
ISBN: 0672328380
EAN: 2147483647
Year: 2007
Pages: 288

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net