Code Snippet: Count the number of files with an extention

I wanted to know how many files that ended in either .test.ts or .test.js in a directory, came up with this guy:

tree --noreport --gitignore -i | grep -c -E '\.test\.(js|ts)' 

Tree

tree is a tool for listing out the contents of a directory in a tree-style format.

  • --noreport Removes a summary line at the bottom ("10 directories, 35 files")
  • --gitignore Looks for a .gitignore file and excludes those directories, useful for those pesky node_modules directories
  • -i Removes the whitespace and indent (makes it better input for regex)

Grep

Grep is a tool for searching text against a regex.

We pass in the input from tree into Regex, and only look for files that match a pattern

  • -c for "count", i.e. just tell me how many matches you found
  • -Efor extended regex, it makes the (js|ts) fragment work
See other articles