Linux grep FAQ: How can I use the Unix grep
command to search for multiple patterns with one command?
A little while ago I needed to search for all files in a CVS repository that contain multiple character patterns. That is, I wanted to be able to grep
multiple patterns in multiple files. In my case the patterns I was looking for were the strings "prevayl" and "jtable".
grep multiple string patterns in many files
I couldn't find a way to do this with a simple combination of the find and grep commands, but a friend suggested this grep/find command combination:
grep -li "jtable" $(find . -name "*.java,v" -exec grep -li "prevayl" {} \;)
This find/grep command begins inside the parentheses by first finding all files whose name ends with the characters "*.java,v" and also contains the string "prevayl". Once it finds those files the find
command prints the file names, and then the grep
command at the beginning of the expression kicks in to find the string "jtable".
There may be better ways of doing this, but for today it worked as an okay solution to grep
multiple patterns on many files, in many directories.
Related grep command content
I just updated my Linux grep command examples, and it shows many other grep
command examples that may also be helpful.