Take the 2-minute tour ×

I have a file where it contains a list of search patterns (searchPattern.txt). Its contents is similar to the contents below where there are 3000+ of them.


What I wanted to do is to use grep and search directories using the patterns listed in the file if they exist. It would be similar to this kind of command but instead of one search string there are many and its listed in a file.

grep searchPattern.txt diagnostics*

Although the command above doesn't work its just an idea as to what I wanted to happen. Can this be done with grep? If it can't be done can someone suggest a better way to do this?

share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

Try this one:

grep -r -f /path/to/pattern/file diagnostics*
share|improve this answer
hi tink this one doesn't work. –  dimas May 3 '13 at 3:43
@dimas: maybe I didn't quite understand your question: you have a file with multiple patterns that you would like to search for in a directory structure where each directory is named diagnostics{something-or-other}? Or are you trying to find files under the directory whose name contains a pattern? –  tink May 3 '13 at 3:48
Yup the first one. –  dimas May 3 '13 at 3:52
@dimas have you checked with find and xargs ? –  Rahul Patil May 3 '13 at 3:54
So in what way is grep -f failing? –  tink May 3 '13 at 3:54
show 7 more comments

This is probably work with all linux variants:

$ grep -rFf searchPattern.txt directory/
share|improve this answer
add comment

Your Answer


