Using grep recursively
Just use the --include parameter, like this:
grep -inr --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP email@domain.example
That should do what you want.
To take the explanation from HoldOffHunger’s answer below:
grep: command-r: recursively-i: ignore-case-n: each output line is preceded by its relative line number in the file--include \*.cpp: all *.cpp: C++ files (escape with \ just in case you have a directory with asterisks in the filenames)./: Start at current directory.
ShareFollow

22.8k1313 gold badges8686 silver badges105105 bronze badges
answered Sep 20, 2012 at 16:35
48.4k88 gold badges6565 silver badges8181 bronze badges
- 1@Hong where is the documentation that -R is for symbolic links? – titus Jan 20, 2016 at 13:46
- Can someone explain why this syntax works as opposed to what’s described in the man page
--include=GLOB– Eaten by a Grue Jan 21, 2016 at 16:15 - 11This example seems to have a high score because it covers such a wide range of possibilites but the answer given below of grep -r –include=*.txt ‘searchterm’ ./ really explains the essence of the answer – David Casper Jan 27, 2017 at 1:44
- 16why not use double quotes instead of backslash? e.g:
grep -r -i --include="*.h" --include="*.cpp" CP_Image– pambda Apr 11, 2017 at 5:15 - @nelson How would you change this to just return all file extensions – Daniel Kaplan Jul 10, 2019 at 23:35
433
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.
Source: PHP Revolution: How to Grep files in Linux, but only certain file extensions?
ShareFollow
answered Feb 8, 2016 at 22:46
17.8k88 gold badges9494 silver badges127127 bronze badges
- 9You should escape the
*using\*.cppor'*.cpp'. Otherwise it won’t give the expected result when the working directory contains some*.txtfiles. – Melebius Jan 2, 2017 at 7:17 - @Melebius can you explain why it needs escaping – does it have anything to do with the CPP or TXT extensions you mentioned? Or did you just use those as examples? – Simon East Apr 28, 2017 at 3:05
- 2@SimonEast These extensions are those used in this question and answer, nothing special otherwise. It would probably work without escaping when using
--include=<pattern>but it is important to escape*with--include <pattern>(a space instead of=) which feels very similar otherwise. – Melebius Apr 28, 2017 at 6:55 - @Melebius adding to what you wrote, it does work with
--include=<pattern>. It also works with--include<pattern>, so long as there are no files matching the pattern in the current directory. I.e., it’s safest to escape the pattern when you’re not using the=syntax, but you can live dangerously if you assume there are no files matching the pattern in the current directory. – TooTone Nov 4, 2021 at 22:02
