4

right now i am using something like this:

find . -name "*.xml" | xargs grep -l "foobar"

it works, but i was wondering if grep has this functionality built in?

numan
  • 143
  • 2

3 Answers3

7

POSIX grep doesn't have such options, but coreutils grep does.

grep -r --include='*.xml' -l foobar .

should do it.

FreeBSD grep (and OS X) appear to have the same thing, Solaris & AIX lack them AFAICT.

Mat
  • 1,536
  • 1
  • 17
  • 21
1

You could also do:

find . -name "*.xml" -exec grep -l "foobar" {} \;
Jason Floyd
  • 1,792
  • 1
  • 13
  • 18
0

You can use ack where that command would be

ack --xml -l foobar

Note that ack's filetypes mean that it doesn't just consider *.xml files. Any file with the extension .xml, .dtd, .xsl, .xslt, or .ent will be selected for searching under the --xml flag. See ack --help-types for a list of types and extensions.

Andy Lester
  • 740
  • 5
  • 16