Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


share|improve this question

migrated from 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
up vote 9 down vote accepted

You can use

grep -rl stringToSearch .


 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


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

Your Answer


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.