Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. 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

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

This is probably work with all linux variants:

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

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.