Saturday, 9 December 2017

linux - grep, but only certain file extensions

itemprop="text">


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?


itemprop="text">
class="normal">Answer



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.



Syntax
notes:





  • -r
    - search recursively

  • -i -
    case-insensitive
    search

  • --include=\*.${file_extension}
    - search files that match the extension(s) or file pattern
    only


No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print ...