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?


Try this one:

grep -r -f /path/to/pattern/file diagnostics*
  • 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 ? 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/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.