Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

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

Wondering if anyone can help me, I'm very rusty with bash and seem to hit a bit of an impasse.

I'm storing a list of strings in a file and would like to read the file and pipe each line returned to grep which in turn searches a directory for files containing the string.

Initial attempt:

cat filename | grep -lr *

However this is not returning any output.

Can anyone give me some directions on the best approach?

share|improve this question

Avoid that useless use of cat. You can of course solve this with xargs and the like. But that's over-complex compared to a simple while loop.

while read i 
    grep -r -- "$i" directory/
done < filename
share|improve this answer
Are you sure about that peth? Pretty sure it finishes after the last line of filename. – PriceChild Jun 2 '11 at 9:21
Ah apologies, never saw the sneaky edit. Never used the pipe up there before either.. – PriceChild Jun 2 '11 at 13:01

I would try this.

cat filename | while read line ; do grep -lr "$line" * ; done

You could also pipe it to "sort -u" so you don't get duplicate.

share|improve this answer

You must log in to answer this question.

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