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.
Assuming these are checked in, using `git grep` this just searches the checked in code. node_modules and vendor.
‘git grep’ will not search ignored files by default, which is usually the case for the node_modules directory.