grep, but only certain file extensions

I am working on writing some scripts to grep certain directories, but these directories contain all sorts of file types.

I want to grep just .h and .cpp for now, but maybe a few others in the future.

So far I have:

{ grep -r -i CP_Image ~/path1/;

grep -r -i CP_Image ~/path2/;

grep -r -i CP_Image ~/path3/;

grep -r -i CP_Image ~/path4/;

grep -r -i CP_Image ~/path5/;} 

| mailx -s GREP email@domain.com

Can anyone show me how I would now add just the specific file extensions?


Just use the --include parameter, like this:

grep -r -i --include *.h --include *.cpp CP_Image ~/path[12345] | mailx -s GREP email@domain.com

that should do what you want.


Some of these answers seemed too syntax-heavy, or they produced issues on my Debian Server. This worked perfectly for me.

grep -r --include=*.txt 'searchterm' ./

...or case-insensitive version...

grep -r -i --include=*.txt 'searchterm' ./
  • grep : command

  • -r : recursively

  • -i : ignore-case

  • --include : all *.txt: text files (escape with just in case you have a directory with asterisks in the filenames)

  • 'searchterm' : What to search

  • ./ : Start at current directory.


  • 怎么样:

    find . -name '*.h' -o -name '*.cpp' -exec grep "CP_Image" {} ; -print
    
    链接地址: http://www.djcxy.com/p/3318.html

    上一篇: Trello如何访问用户的剪贴板?

    下一篇: grep,但只有某些文件扩展名