Bash

How to count number of files in each directory

25 September 2026 · 6 min read

How to count number of files in each directory

Navigating complex file systems and understanding their structure is a fundamental skill for anyone working with computers, from system administrators to casual users. A common task that often arises is the need to accurately count the number of files within specific directories or even across an entire directory tree. This process, often more intricate than a simple glance, provides vital insights into storage usage, data organization, and potential system performance bottlenecks. Learning how to count number of files in each directory efficiently is not just about curiosity; it’s a practical necessity for maintaining order, performing backups, or auditing data. This guide will walk you through various robust methods across different operating systems, ensuring you can precisely determine your file counts with confidence.

Counting Files in Linux and macOS using the Command Line

For users on Unix-like systems such as Linux or macOS, the command line offers powerful and flexible tools for file counting. The elegance of these commands lies in their ability to be combined, allowing for highly specific and customized queries. Understanding the core utilities like ls, find, and wc is crucial for mastering directory enumeration. These tools are often pre-installed, making them readily available for immediate use.

To count files in a single, non-recursive directory, the combination of ls and wc -l is exceptionally effective. The ls -F | grep -v / command lists all entries in the current directory and then filters out directories, leaving only files. Piping this output to wc -l then provides a precise count of those files. For instance, if you’re in a project folder and want to know how many source code files are directly there, this command offers a quick answer without diving into subfolders. This method is fast for top-level counts.

To count files in the current directory (non-recursively), you can use:

ls -F | grep -v / | wc -l

This command lists all files and directories, filters out directories by looking for the ‘/’ suffix added by ls -F, and then counts the lines, which correspond to the number of files. However, for more complex scenarios, especially when you need to recursively count files or apply specific filters, the find command becomes indispensable. It allows you to search for files based on various criteria, such as type, name, or modification date, before counting them. This level of granularity is vital for advanced system management tasks, like finding all log files older than a week or counting only image files within a specific project folder. The find command is a cornerstone of shell scripting for file system analysis.

Counting Files in Windows using PowerShell

Windows users have an equally powerful toolset at their disposal through PowerShell, a robust command-line shell and scripting language. PowerShell cmdlets like Get-ChildItem provide an intuitive and highly functional way to interact with the file system, making it straightforward to count files in both single directories and recursively across entire trees. Its object-oriented nature allows for easy filtering and manipulation of output, which is a significant advantage over traditional command prompt methods.

To count files in a specific directory without including subdirectories, the Get-ChildItem cmdlet, combined with the -File parameter, is your primary tool. This command retrieves only file objects, which can then be piped to Measure-Object to get a total count. This is particularly useful when you need to quickly assess the number of items in a particular folder, perhaps before moving or archiving its contents. For example, if you’re managing a download folder, this command can show you how many individual files are present, ignoring any subfolders. The consistency of PowerShell commands across different tasks simplifies learning and usage.

To count files in a specific directory (non-recursively) in PowerShell:

Get-ChildItem -Path "C:\Your\Directory\Path" -File | Measure-Object | Select-Object Count

This command gets only file items within the specified path and then counts them. For more advanced scenarios, such as needing to count files recursively through all subdirectories, PowerShell excels with its -Recurse parameter. This capability is essential for operations like auditing an entire drive’s contents or generating comprehensive reports on specific file types across multiple project folders. The flexibility of PowerShell allows for easy integration into larger scripts, enabling automated file system analysis and reporting. According to Microsoft’s PowerShell documentation, Get-ChildItem is designed for versatility in listing and filtering file system objects, making it a cornerstone for managing directories efficiently.

Recursive File Counting in All Subdirectories

Often, simply counting files in a single directory isn’t enough. Many tasks require a comprehensive count that includes all files nested within subdirectories, sometimes several layers deep. This recursive counting is crucial for understanding the total data footprint of a project, identifying areas with excessive small files, or preparing for a large-scale data migration. Both Linux/macOS and Windows provide robust methods for achieving this, though their syntax differs significantly.

In Linux and macOS, the find command is the go-to utility for recursive file counting. Its power lies in its ability to traverse directory trees and execute actions based on various criteria. To count all files recursively from a starting directory, you would typically use find . -type f | wc -l. Here, . signifies the current directory, -type f specifies that only files should be considered (excluding directories, symbolic links, etc.), and wc -l counts the lines of output, giving you the total number of files. This method is incredibly versatile and can be extended with additional filters for file names, sizes, or modification times. For detailed options on the find command, consulting the man pages is highly recommended.

To count all files recursively from the current directory (Linux/macOS):

find . -type f | wc -l

For Windows PowerShell, the -Recurse parameter with Get-ChildItem makes recursive counting straightforward. The command Get-ChildItem -Path "C:\Your\Directory" -File -Recurse | Measure-Object | Select-Object Count will list all files within the specified directory and all its subdirectories, then Question & Answer :

I am able to list all the directories by

find ./ -type d 

I attempted to list the contents of each directory and count the number of files in each directory by using the following command

find ./ -type d | xargs ls -l | wc -l 

But this summed the total number of lines returned by

find ./ -type d | xargs ls -l 

Is there a way I can count the number of files in each directory?

This prints the file count per directory for the current directory level:

du -a | cut -d/ -f2 | sort | uniq -c | sort -nr