
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
file-counter
Advanced tools
Recursively count the number of files within a directory, and all of its subdirectories. Apply filters to exclude files or directories from the total.
Recursively count the number of files within a directory, and all of its subdirectories. Apply filters to exclude files or directories from the total.
file-counter can be used as either a command line utility or included in your
projects or scripts where you need to know the number of files within a directory.
Install globally so you can run the file-counter command via the command line
within a directory to get the total number of files within that directory and all
sub-directories.
Via npm:
npm install -g file-counter
countFiles MethodIf you need to get the total number of files for a task progress bar, or you’re
evaluating the number of files within a directory for maintenance purposes within
another script, you can use the countFiles method.
Via npm:
npm install file-counter
Via yarn:
yarn add file-counter
You can use file-counter two different ways, as a command line utility or as a
utility for use within your package or project. Below are a few examples on how
you can use it in those scenarios.
$ file-counter [options] [dir]
The following options are available both for the CLI use of file-counter, as
well as for the countFiles method. Please note that when using the --exclude-*,
or excludeDirs, options that the defaults will no longer be applied.
-f, --exclude-files [...files]Depending on what you’re doing, you may find the need to exclude certain files from the total that is returned. You can do that by including a comma separated list of file names or regular expressions to exclude from the count tally.
By default, the following files are excluded from totals:
.DS_Store.DS_Store?.Spotlight-V100.Trashesehthumbs.dbThumbs.dbHere’s an example of using the -f, or --exclude-files, option:
Exlude any .DS_Store file and all Javascript (*.js) files:
$ file-counter -f '.DS_Store,(.*).js'
-d, --exclude-dirs [...directories]The same goes for directories. You can use either the exact directory name or a regular expression to exclude directories, and their files, from being included in the total count.
By default, the following directories are excluded from totals:
.gitnode_modulesvendorHere are a few examples of using the -d, or --exclude-dirs, option:
Exclude .git and node_modules directories:
$ file-counter -d '.git,node_modules'
Exclude all directories (ie. only tally the files in the root directory):
$ file-counter -d '(.*)'
-v, --verboseIncluding the --verbose flag will output file names included in the total count.
$ file-counter -v
countFiles MethodIn addition to the command line utility, you can also use the method that the utility relies on in your own projects and scripts where you would want to know the number of files in a directory.
countFiles({...options}, count = 0)
*Note: count is there because the method is recursively called while traversing
the directories. Please do not prepopulate that argument unless you want to
increment/decrement the count by that value.
countFiles({
dir: '...',
excludeFiles: ['...',],
excludeDirs: ['...',],
includeLogs: [true|false],
verbose: [true|false],
})
Here’s an example where we use the countFiles method in combination with the
progress package to show a progress
bar during the processing of a Node script.
import path from 'path';
import { countFiles } from 'file-counter';
import ProgressBar from 'progress';
const ROOT_DIR = path.join(__dirname, '../');
const totalFiles = countFiles({
dir: ROOT_DIR,
});
const bar = new ProgressBar(':bar', { total: totalFiles });
...
(iterate over files, doing stuff)
bar.tick();
...
And, if you’re curious about the progress package referenced above, you can find
out more about that, here.
FAQs
Recursively count the number of files within a directory, and all of its subdirectories. Apply filters to exclude files or directories from the total.
We found that file-counter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.