Grep in files with a specific extension under a directory

11

3

Is there a command that allows searching a keyword in files under a directory with specific extension ?

The string grep -irn "string" ./path gives a recursive search for all files under the directory./path. My specific requirement is to search in all files under ./path with an extension such as *.h

Ginu Jacob

Posted 2016-09-08T03:49:12.580

Reputation: 321

Answers

11

After some trials, I think grep -irn 'string' --include '*.h' is more handy

Ginu Jacob

Posted 2016-09-08T03:49:12.580

Reputation: 321

1If you're going to do that, be sure to put the "*.h" in quotes. – G-Man Says 'Reinstate Monica' – 2016-09-08T04:12:16.913

It works with and without quotes – Ginu Jacob – 2016-09-08T04:43:52.087

3Now it does. Try creating a file called foobar.h in the current directory, and try it again. – G-Man Says 'Reinstate Monica' – 2016-09-08T05:56:36.300

10

Set (turn on) the shell option globstar with the command

    shopt -s globstar

This will cause ** as a filename component to mean everything here and below.  So path/** means everything in the path directory and its subdirectories.  (You don't need to type ./ here.)  Then you can use

grep -in "string" path/**/*.h

to search all the .h files in and under path.


You can unset options with shopt -u.

G-Man Says 'Reinstate Monica'

Posted 2016-09-08T03:49:12.580

Reputation: 6 509

5

find /path -iname "*.h" -exec grep -inH "string" "{}" \;

Ipor Sircer

Posted 2016-09-08T03:49:12.580

Reputation: 3 578

1-exec ... + is good, too. – G-Man Says 'Reinstate Monica' – 2016-09-08T03:56:21.537

1I just noticed — you forgot to include "string" in your answer! – G-Man Says 'Reinstate Monica' – 2016-09-08T16:29:34.497

3

If you can install something on your machine, I suggest using ack.

You can do exactly what you need with it and much more. For your use case, you can do:

# Depending of your system, you have to use one or the other
ack --hh -i string path
ack-grep --hh -i string path
  • --hh filters on h files
  • -i ignores the case

To find which file filters are supported natively, run the command ack --help=type.

A.D.

Posted 2016-09-08T03:49:12.580

Reputation: 149

There's also ag, a former clone of ack. Former, because their feature sets have since diverged somewhat.

– 8bittree – 2016-09-28T17:24:03.560

I didn't know ag. Thanks for sharing. – A.D. – 2016-09-29T06:58:56.863

3

What about this one?

find -L ./path -name "*.h" -exec grep -in "string" {} \;

Explanation:

  • -L : follow symlinks
  • -name : using the asterisk, you can describe extensions
  • -in : same as your proposal, but the 'r' is replaced by the find command
  • {} : this stands for the result of the find command
  • \; : in case you combine find with -exec, this is the end-of-command specifier

Dominique

Posted 2016-09-08T03:49:12.580

Reputation: 1 013

1How does "*.h" work? I would have thought it needed to be '*.h' – Joe – 2016-09-13T07:15:25.390

I always work with double quotes, I don't think it makes a difference. – Dominique – 2016-09-13T07:22:43.110

0

If you're using gnu grep then it's got a flag that does exactly what you want:

grep -irn --include=\*.h "string" path

although I don't think it's available in other greps.

plnu

Posted 2016-09-08T03:49:12.580

Reputation: 9