Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I need the GREP syntax for the following task (am in a LINUX OS):

querying ALL files in the CURRENT directory which contain STRING but only listing the FILENAMES which contain a match.

Thanks!

share|improve this question

migrated from stackoverflow.com Sep 7 '12 at 13:25

This question came from our site for professional and enthusiast programmers.

    
I think this crosses the line and isn't a programming question. Voting for move to Super User. –  unwind Sep 6 '12 at 12:38
    
I disagree - for me it is common to embed OS commands in other languages. –  RawFocus Sep 6 '12 at 12:53

2 Answers 2

up vote 8 down vote accepted

You can use

grep -rl stringToSearch .

or

 find . -type f -exec grep -l stringToSearch {} \;

For more info on grep and other unix command, please refer to manual ( man )

In that case man grep says that

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

Obviously, as a bash command, if your string contains special chars or spaces, you have to (in order) escape them and/or surrounding your string with quotas

share|improve this answer
    
thanks for rapid response! –  RawFocus Sep 6 '12 at 12:53

"Grep -l" wil give you the list of file names.

> echo "hello" > test_file1.list
> echo "hello2.." > test_file2.list
> echo "xyz" > test_file3.list


> grep "hello" test_file*list

test_file1.list:hello
test_file2.list:hello2..


> grep -l "hello" test_file*list
test_file1.list
test_file2.list
share|improve this answer
    
thanks - this is also helpful –  RawFocus Sep 6 '12 at 13:03

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.