*nix find, exclude node_modules from list

Say you have some tests you want to run using make, and you want to find all the files in your project with .test.js in as the extension. This is typical of a Node/Express application. However, everything in node_modules ships with its own tests (or should do so :), and you probably don't want the overhead of running those tests as well. Unix find supports a number of helpful operations to let you surgically produce a list of test files to run.

Here's a quick way of getting the list without using grep (which could cause you to relinquish access to find's other operations):

find . -name '[regex]' -not ( -wholename '[regex]' )

... if your tests are suffixed with .test.js, and you want to exclude everything in a node_modules directory, you could do:

find . -name '.test.js' -not (-wholename 'node_modules*' )

Tags: