how do I use the grep --include option for multiple file types?

tianyapiaozi picture tianyapiaozi · May 16, 2012 · Viewed 61.3k times · Source

When I want to grep all the html files in some directory, I do the following

grep --include="*.html" pattern -R /some/path

which works well. The problem is how to grep all the html,htm,php files in some directory?

From this Use grep --exclude/--include syntax to not grep through certain files, it seems that I can do the following

grep --include="*.{html,php,htm}" pattern -R /some/path

But sadly, it would not work for me.
FYI, my grep version is 2.5.1.

Answer

Steve picture Steve · May 17, 2012

You can use multiple --include flags. This works for me:

grep -r --include=*.html --include=*.php --include=*.htm "pattern" /some/path/

However, you can do as Deruijter suggested. This works for me:

grep -r --include=*.{html,php,htm} "pattern" /some/path/

Don't forget that you can use find and xargs for this sort of thing to:

find /some/path/ -name "*.htm*" -or -name "*.php" | xargs grep "pattern"

HTH