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

Solution 1:

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

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

Solution 2:

Using {html,php,htm} can only work as a brace expansion, which is a nonstandard (not POSIX-compliant) feature of bash, ksh, and zsh.

  • In other words: do not try to use it in a script that targets /bin/sh - use explicit multiple --include arguments in that case.

  • grep itself does not understand {...} notation.

For a brace expansion to be recognized, it must be an unquoted (part of a) token on the command line.

A brace expansion expands to multiple arguments, so in the case at hand grep ends up seeing multiple --include=... options, just as if you had passed them individually.

The results of a brace expansion are subject to globbing (filename expansion), which has pitfalls:

  • Each resulting argument could further be expanded to matching filenames if it happens to contain unquoted globbing metacharacters such as *.
    While this is unlikely with tokens such as --include=*.html (e.g., you'd have to have a file literally named something like --include=foo.html for something to match), it is worth keeping in mind in general.

  • If the nullglob shell option happens to be turned on (shopt -s nullglob) and globbing matches nothing, the argument will be discarded.

Therefore, for a fully robust solution, use the following:

grep -R '--include=*.'{html,php,htm} pattern /some/path
  • '--include=*.' is treated as a literal, due to being single-quoted; this prevents inadvertent interpretation of * as a globbing character.

  • {html,php,htm}, the - of necessity - unquoted brace expansion[1] , expands to 3 arguments, which, due to {...} directly following the '...' token, include that token.

  • Therefore, after quote removal by the shell, the following 3 literal arguments are ultimately passed to grep:

    • --include=*.html
    • --include=*.php
    • --include=*.htm

[1] More accurately, it's only the syntax-relevant parts of the brace expansion that must be unquoted, the list elements may still be individually quoted and must be if they contain globbing metacharacters that could result in unwanted globbing after the brace expansion; while not necessary in this case, the above could be written as
'--include=*.'{'html','php','htm'}