Recursive grep fails for *.c files

I suggest to use the --include option of grep:

grep -lr --include='*.c' search-pattern .

The *.c pattern is evaluated by your shell. It applies to the current directory, just like you would using ls *.c.

I think what you want instead is to find all files matching the *.c pattern (recursively) and have grep search for you in it. Here's a way to do that:

find . -name "*.c" -print0 | xargs --null grep -l search-pattern

It uses xargs to append the search results by find.


Alternatively, use the -exec option to find, e.g.:

find . -name "*.c" -exec grep -l search-pattern "{}" \;

Also, I'm not sure if you really want the -l option to grep. It will stop at the first match:

-l, --files-with-matches
      Suppress normal output; instead print the name of  each
      input  file  from which output would normally have been
      printed.  The scanning will stop on  the  first  match.
      (-l is specified by POSIX.)

Tags:

Grep