How can I exclude directories from grep -R?

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


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

  • The GNU man page for File and Directory Selection
  • A related StackOverflow answer Use grep –exclude/–include syntax to not grep through certain files

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!

Leave a Comment