How to Find All Test Files in Terminal

Task

Find all test files within a big project folder and print out the list of these files. Any file containing the word “test” in its name will be recognized as a test file, except for those in the node_modules folder.

Solution

Even though the project folder structure may be quite complex, accomplishing this task is surprisingly easy with a basic terminal command

find . -type f -not -path "*node_modules*" -name "*test*" -print
Terminal

Explanation

  • Specifies the starting directory for the search, in this case, the current directory.
  • -type f: Restricts the search to only files (not directories).
  • -not -path "*node_modules*": Excludes any files with “node_modules” in their path. The -not predicate negates the condition, and -path specifies the path pattern to exclude.
  • -name "*test*": Specifies files with “test” in their name.
  • -print: Prints the names of the files that match the specified criteria.

With this command, you’ll search for files with “test” in their name, excluding any files with “node_modules” in their path.

Output Results

./src/__test__/apple.test.tsx
./src/init.app.test.ts
./src/components/Grids/helpers.test.ts
./src/__tests__/placeholder.test.tsx
./src/__test__/example.test.tsx
./src/components/Cropper/custom-event.test.tsx
./src/index.test.js

Useful Resources

One Reply

Leave a Reply

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