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

后端 未结 7 1875
故里飘歌
故里飘歌 2020-12-04 10:40

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. T

相关标签:
7条回答
  • 2020-12-04 11:03

    Try this. -r will do a recursive search. -s will suppress file not found errors. -n will show you the line number of the file where the pattern is found.

        grep "pattern" <path> -r -s -n --include=*.{c,cpp,C,h}
    
    0 讨论(0)
  • 2020-12-04 11:04

    Use grep with find command

    find /some/path -name '*.html' -o -name '*.htm' -o -name '*.php' -type f 
     -exec grep PATTERN {} \+
    

    You can use -regex and -regextype options too.

    0 讨论(0)
  • 2020-12-04 11:09

    It works for the same purpose, but without --include option. It works on grep 2.5.1 as well.

    grep -v -E ".*\.(html|htm|php)"
    
    0 讨论(0)
  • 2020-12-04 11:10

    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

    0 讨论(0)
  • 2020-12-04 11:16

    Try removing the double quotes

    grep --include=*.{html,php,htm} pattern -R /some/path
    
    0 讨论(0)
  • 2020-12-04 11:16

    is this not working?

      grep pattern  /some/path/*.{html,php,htm} 
    
    0 讨论(0)
提交回复
热议问题