Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Count Files in Subfolders: Ways to Get Totals

· 6 min read · Home Tech

In this article
  1. Why a Simple Folder View Isn’t Enough
  2. Method 1: Using File Explorer (Windows) with Search
  3. Method 2: Using Terminal (macOS/Linux) with find
  4. Method 3: Using PowerShell (Windows) for More Control
  5. Method 4: Using Command Prompt (Windows) with dir
  6. Common Mistakes and How to Avoid Them
  7. Comparing the Methods: Which Should You Use?
  8. Frequently Asked Questions

To count files in subfolders, you need to include the subfolders in your search or use a recursive command, since a basic folder view only shows the top-level items. This article explains the exact methods for Windows, macOS, and Linux, plus how to avoid common pitfalls that lead to wrong totals.

Why a Simple Folder View Isn’t Enough

When you open a folder in File Explorer (Windows) or Finder (macOS), the status bar or window footer typically shows the number of items in that folder only—not the files inside its subfolders. For example, a folder named “Projects” might show “18 items” when it actually contains 1,247 files spread across 30 subfolders. This happens because the operating system counts the immediate contents (files and subfolders) but does not recursively descend into each subfolder unless you explicitly ask it to.

The same limitation applies to command-line tools if you forget the recursive flag. The ls command on Linux/macOS lists the current directory’s contents; dir on Windows does the same. To get a true total of every file in every nested folder, you need a recursive operation.

Windows File Explorer has a built-in search that can count files recursively without any extra software. Here’s the step-by-step:

  1. Open the parent folder whose subfolders you want to count.
  2. Click inside the search box (top-right corner).
  3. Type * (asterisk) to match all files, or type a specific extension like *.jpg for only images.
  4. Press Enter. The search will scan the current folder and all subfolders.
  5. Look at the status bar at the bottom of the window. It shows “X items” – that number is the total file count, including files in subfolders.

Note: The search may take a few seconds for large folder trees. If you want to exclude certain file types, you can refine the search query (e.g., *.* NOT *.tmp). This method works on Windows 10 and Windows 11.

Method 2: Using Terminal (macOS/Linux) with find

On macOS and Linux, the find command is the most reliable way to count files recursively. Open Terminal and navigate to the parent folder, or provide the full path. The basic command is:

find /path/to/folder -type f | wc -l

Breaking it down:

  • find /path/to/folder – starts the search from that folder and descends into all subfolders.
  • -type f – limits results to regular files (not directories, symlinks, or special files).
  • | wc -l – pipes the output to wc -l, which counts the number of lines (each line is one file).

If you want to count only files with a certain extension, add -name "*.jpg":

find /path/to/folder -type f -name "*.jpg" | wc -l

For a count that includes hidden files (files starting with a dot), use find /path/to/folder -type f -not -path '*/.*' | wc -l to exclude them, or just use the plain command which includes hidden files by default.

Method 3: Using PowerShell (Windows) for More Control

PowerShell gives you a more flexible and scriptable approach than File Explorer’s search. Open PowerShell and run:

(Get-ChildItem -Path "C:\Your\Folder" -Recurse -File).Count

Explanation:

  • Get-ChildItem – lists items.
  • -Recurse – includes all subfolders.
  • -File – filters to files only (excludes directories).
  • .Count – returns the number of items in the resulting collection.

To count a specific file type, add -Include *.jpg or use -Filter *.jpg (faster). For example:

(Get-ChildItem -Path "C:\Your\Folder" -Recurse -File -Filter *.jpg).Count

PowerShell also lets you count files by size, date, or other properties, which is useful for audits.

Method 4: Using Command Prompt (Windows) with dir

Command Prompt (cmd.exe) can also give a recursive count, though the output is less clean. Run:

dir /s /b "C:\Your\Folder" | find /c /v ""

What it does:

  • dir /s – includes subdirectories.
  • /b – uses bare format (just the full path of each file).
  • | find /c /v "" – counts the number of lines (each line is a file or folder).

Note: This counts both files and folders. To count only files, you’d need to filter out lines that end with a backslash (folders), which is awkward in cmd. For that reason, PowerShell is recommended for accurate file-only counts.

Common Mistakes and How to Avoid Them

Several errors can give you a misleading count:

  • Forgetting the recursive flag: Using ls or dir without -R//s only counts the current folder. Always double-check your command includes recursion.
  • Counting folders as files: The dir /s /b method includes subfolder names. If you need only files, use PowerShell’s -File flag or find -type f.
  • Hidden files: On macOS/Linux, find includes hidden files by default, which might surprise you. If you want to exclude them, add a filter.
  • Symbolic links: find by default does not follow symlinks to directories. If your folder contains symlinks pointing to other folders, the count may be lower than expected. Add -L to follow them, but be aware this can cause infinite loops.
  • File permissions: If you don’t have read permission on a subfolder, the command may skip it silently or show an error. Run the command with appropriate privileges if you need a complete count.

Comparing the Methods: Which Should You Use?

The best method depends on your situation:

  • Quick, one-time count on Windows: File Explorer search is the easiest – no typing commands.
  • Scripting or automation: PowerShell (Windows) or find (macOS/Linux) are ideal because you can save them in scripts and reuse them.
  • Counting files with specific attributes (size, date): PowerShell or find with -size, -mtime etc. give you that control.
  • Very large folder trees (millions of files): Command-line tools are faster than GUI search because they don’t render a graphical list.

For most users, the File Explorer search method is sufficient. For IT professionals or developers, the command-line methods are more precise and scriptable.

Frequently Asked Questions

Does the count include files in hidden subfolders?

It depends on the method. File Explorer search includes hidden files and folders by default. find on Linux/macOS includes hidden files but does not enter hidden directories unless you use -L or explicitly include them. PowerShell’s Get-ChildItem excludes hidden items by default; add -Force to include them.

How do I count only files, not folders?

Use the methods with explicit file filters: find -type f (macOS/Linux), (Get-ChildItem -File).Count (PowerShell), or the File Explorer search with * which shows only files (folders are not listed as results). The dir /s /b method includes folders, so avoid it if you need pure file counts.

Why does File Explorer show a different number than my command line?

File Explorer’s status bar sometimes shows “items” (files + folders) when you select a folder, and the search may exclude certain system files or hidden items depending on your settings. Command-line tools like find include everything unless you filter. Also, File Explorer’s search may not index all file types if you’re using a non-indexed location – it falls back to a slow crawl but still counts everything.

Can I count files in subfolders on a network drive?

Yes, the same commands work on mapped drives or UNC paths. For example, find \\server\share\folder -type f | wc -l works on macOS/Linux if the drive is mounted. On Windows, use PowerShell with the UNC path: (Get-ChildItem -Path "\\server\share\folder" -Recurse -File).Count. Be aware that network latency can make the operation slow.

Is there a way to count files without opening a terminal?

Yes, on Windows you can use File Explorer’s search as described. On macOS, you can use Finder’s search: press Cmd+F, set the search to the folder, and then look at the “Results” count at the bottom of the window. However, Finder’s search may not count every file if it relies on Spotlight indexing; for a definitive count, use Terminal.

What if I need to count files across multiple separate folders?

You can run the command on each folder and add the results, or use a script that loops through a list of paths. For example, in PowerShell: @("C:\Folder1","D:\Folder2") | ForEach-Object { (Get-ChildItem $_ -Recurse -File).Count } | Measure-Object -Sum gives a total sum.