Recursive grep is pretty handy way to searching a pattern in a directory recursively. Sometimes it is useful to restrict search to specific file pattern (e.g. *.php). One can use the following command for this:
$ grep -r --include="*.php" pattern /path/to/dir
Note that –include=”GLOB” skip files whose base name matches GLOB (using wildcard matching). A file-name glob can use *, ?, and […] as wildcards, and \ to quote a wildcard or backslash character literally.
In case you want to include multiple pattern, you can repeat –include as shown below:
$ grep -r --include="*.php" --include="*.html" pattern /path/to/dir
In case you want to exclude specific file patterns (e.g images, etc.) you can use –exclude as shown below:
$ grep -r --exclude="*.gif" --exclude="*.png" pattern /path/to/dir