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

14 s
14

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

  • 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 Reply

Your email address will not be published. Required fields are marked *