How can I exclude directories from grep -R?

TIMEX picture TIMEX · Jul 3, 2011 · Viewed 453.4k times · Source

I want to traverse all subdirectories, except the "node_modules" directory.

Answer

Johnsyweb picture Johnsyweb · Jan 1, 2012

Recent versions of GNU Grep (>= 2.5.2) provide:

--exclude-dir=dir

which excludes directories matching the pattern dir from recursive directory searches.

So you can do:

grep -R --exclude-dir=node_modules 'some pattern' /path/to/search

For a bit more information regarding syntax and usage see

For older GNU Greps and POSIX Grep, use find as suggested in other answers.

Or just use ack (Edit: or The Silver Searcher) and be done with it!