Exclude a Directory when Grepping

As a developer-of-another-discipline who is now transitioning into a bunch of NodeJS projects, I grep a LOT. However I am usually only interested in the code in the project at hand, and not the dependencies – of course grep has a switch for that! To grep your project and not the dependencies in the node-modules folder, try this:

grep -R --exclude-dir node_modules [what to search for] * 

If you’re using a different tech stack you may want to exclude a different directory (for PHP, the directory would be called vendor), but this is a very handy tip and a bit nicer than the older approach I was using which did the whole search and then used a second grep to eliminate things by using the -v switch.

2 thoughts on “Exclude a Directory when Grepping

  1. Assuming these are checked in, using `git grep` this just searches the checked in code. node_modules and vendor.

Leave a Reply

Please use [code] and [/code] around any source code you wish to share.

This site uses Akismet to reduce spam. Learn how your comment data is processed.