1413

I'm working on a script to grep certain directories:

{ 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 protected]

How can I limit results only to extensions .h and .cpp?

6
  • 14
    Tried grep -r -i CP_Image ~/path1/*.{h,cpp}?
    – user529758
    Sep 20, 2012 at 16:31
  • 11
    Use The Silver Searcher: ag -i CP_Image ~/path[1-5] | mailx -s GREP [email protected]. Job done.
    – johnsyweb
    Oct 9, 2013 at 17:25
  • possible duplicate of grep --exclude/--include syntax (do not grep through certain files)
    – johnsyweb
    Oct 9, 2013 at 17:48
  • Use egrep (is most likely pre-installed on your system), and then you can use a regex.
    – Dogweather
    Oct 9, 2013 at 17:53
  • 12
    The GNU guys really messed up when they added -r to grep to have it search for files as that breaks the UNIX mantra of having tools that "do one thing and do it well". There's a perfectly good tool for finding files with a VERY obvious name.
    – Ed Morton
    Oct 9, 2013 at 17:58

12 Answers 12

1896

Just use the --include parameter, like this:

grep -inr --include \*.h --include \*.cpp CP_Image ~/path[12345] | mailx -s GREP [email protected]

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.

6
  • 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 Jan 21, 2016 at 16:15
  • 11
    This 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 Jan 27, 2017 at 1:44
  • 18
    why 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 Jul 10, 2019 at 23:35
446

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?

5
  • 9
    You should escape the * using \*.cpp or '*.cpp'. Otherwise it won’t give the expected result when the working directory contains some *.txt files.
    – 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
  • Did not answer the question! Jasmine asked for two file types in a single search!
    – John
    Oct 25, 2022 at 2:05
91
grep -rnw "some thing to grep" --include=*.{module,inc,php,js,css,html,htm} ./
8
  • 3
    grep -rn "some thing to grep" --include=*.{module,inc,c,h} *
    – ashish
    Aug 4, 2015 at 11:12
  • 4
    Nice answer. Cleaner than the accepted on IMO but you should add search criteria as @ashish noted Jan 21, 2016 at 16:20
  • why is --include option after needle, not with other options?
    – vladkras
    Sep 16, 2016 at 12:21
  • @vladkras, what do you mean needle? Is it --? Jan 7, 2020 at 22:37
  • 2
    Almost, but that didn't work for me--it kept trying to match on --include=*.foo. The working solution was wrapping the --include value in quotes. E.g. --include="*.foo".
    – Alan Hape
    Mar 3, 2021 at 16:39
62

Use:

find . -name '*.h' -o -name '*.cpp' -exec grep "CP_Image" {} \; -print
4
  • 7
    i'd suggest grouping those -name arguments. strange things can happen if you don't. find . \( -name '*.h' -o -name '*.cpp' \) -exec grep "CP_Image" {} \; -print Sep 20, 2012 at 21:13
  • 2
    use with additional "-type f" to ignore all directory objects, only interested in files. Mar 18, 2017 at 12:32
  • 2
    I used this method for years and it works but it's a LOT slower than recursive grep since find's exec spawns a separate grep process for each file to be searched.
    – beaudet
    Jan 8, 2018 at 19:00
  • 1
    Addressing @beaudet's comment, find can optionally bundle arguments, reducing invocations of the called process to a minimum. find . \( -name \*.h -o -name \*.cpp \) -exec grep -H CP_Image {} + This is suggested but not highlighted in @fedorqui's answer below and is a worthwhile improvement. The -H argument to grep here is useful when find only identifies a single matching file. This could eliminate the usage of -print in the answer. If your total list of files is sufficiently small, using a recursive shell glob (eg. {path1,path2}/**/*.{cpp,h}) might be preferable.
    – Malcolm
    Feb 22, 2019 at 9:51
36

There isn't any -r option on HP and Sun servers, but this way worked for me on my HP server:

find . -name "*.c" | xargs grep -i "my great text"

-i is for case insensitive search of string.

7
  • 2
    I've come across several servers for web hosting companies that do not have the --include option available for fgrep and this is the command line that I use in those instances.
    – Borgboy
    Jan 18, 2016 at 21:30
  • The --include option is also not available when using Git for Windows (MinGW/MSys). Jan 19, 2016 at 14:21
  • @DarrenLewis available in Git Bash for Windows. But strangely, it adds colorful aliases like ll but does not add --color=auto to grep.
    – Xeverous
    Dec 28, 2017 at 17:28
  • This should be the accepted answer for completeness, portability, and brevity! Jan 9, 2019 at 20:30
  • 1
    Re "HP and Sun servers": Do you mean for HP-UX and Solaris? Apr 24, 2021 at 21:13
23

This answer is good:

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

But it can be updated to:

grep -r -i --include \*.{h,cpp} CP_Image ~/path[12345] | mailx -s GREP [email protected]

Which can be simpler.

6
  • 1
    What does "The below answer" refer to? References to relative positions of answers are not reliable as they depend on the view (votes/oldest/active) and changing of the accepted answer and change over time (for votes, active, and accepted state). Please respond by editing your answer, not here in comments (without "Edit:", "Update:", or similar - the answer should appear as if it was written today). Apr 24, 2021 at 21:21
  • Can we simply use grep command along with ls command?
    – Harsha
    Oct 27, 2021 at 15:30
  • @Harsha Sure. use pipe to make the output of ls as the input of grep. such as ls | grep *.h
    – hao
    Dec 25, 2021 at 12:26
  • Could you provide me a link that would give explanations as to how I can fully understand this?
    – Harsha
    Dec 26, 2021 at 10:43
14

Since this is a matter of finding files, let's use find!

Using GNU find you can use the -regex option to find those files in the tree of directories whose extension is either .h or .cpp:

find -type f -regex ".*\.\(h\|cpp\)"
#            ^^^^^^^^^^^^^^^^^^^^^^^

Then, it is just a matter of executing grep on each of its results:

find -type f -regex ".*\.\(h\|cpp\)" -exec grep "your pattern" {} +

If you don't have this distribution of find you have to use an approach like Amir Afghani's, using -o to concatenate options (the name is either ending with .h or with .cpp):

find -type f \( -name '*.h' -o -name '*.cpp' \) -exec grep "your pattern" {} +
#            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

And if you really want to use grep, follow the syntax indicated to --include:

grep "your pattern" -r --include=*.{cpp,h}
#                      ^^^^^^^^^^^^^^^^^^^
1
  • 1
    I needed a script which returned exit status 1 if the grep command matched any files. I started out using find+xargs+grep. But the fact that xargs returns exit status 123 if grep returns 1 made things more complicated. It was more straightforward to just use grep in my case. Mar 10, 2022 at 9:16
11

The easiest way is:

find . -type  f -name '*.extension' 2>/dev/null | xargs grep -i string

Add 2>/dev/null to kill the error output.

To include more file extensions and grep for password throughout the system:

find / -type  f \( -name '*.conf' -o -name "*.log" -o -name "*.bak" \) 2>/dev/null |
xargs grep -i password
5

ag (the silver searcher) has pretty simple syntax for this

       -G --file-search-regex PATTERN
          Only search files whose names match PATTERN.

so

ag -G *.h -G *.cpp CP_Image <path>
1
  • using ag 2.2.0, i needed to put my flags last: ag _string_to_find_ -G _filename_regex_
    – ryanrain
    Apr 20, 2020 at 13:31
2

You should write "-exec grep " for each "-o -name ":

find . -name '*.h' -exec grep -Hn "CP_Image" {} \; -o -name '*.cpp' -exec grep -Hn "CP_Image" {} \;

Or group them by ( )

find . \( -name '*.h' -o -name '*.cpp' \) -exec grep -Hn "CP_Image" {} \;

Option '-Hn' shows the file name and line.

1

Here is a method I normally use to find .c and .h files:

tree -if | grep \\.[ch]\\b | xargs -n 1 grep -H "#include"

Or if you need the line number as well:

tree -if | grep \\.[ch]\\b | xargs -n 1 grep -nH "#include"
0
0

If you want to filter out extensions from the output of another command e.g. "git":

files=$(git diff --name-only --diff-filter=d origin/master... | grep -E '\.cpp$|\.h$')

for file in $files; do
    echo "$file"
done

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.