How to Count Files and Folders in a Directory Using Terminal on a Mac

Illustration for How to Count Files and Folders in a Directory Using Terminal on a Mac

If you need to know how many files or folders are inside a directory, Terminal can give you an exact count without opening each item manually. The simplest command counts visible entries in the current directory:

find . -type f | wc -l

This counts regular files in the current directory and all of its subdirectories. To count folders instead, use:

find . -type d | wc -l

Replace the period (.) with a folder path when you want to count a different directory.

Quick Answer

  1. Open Terminal from Applications > Utilities.
  2. Type cd, add a space, and drag the target folder into the Terminal window.
  3. Press Return.
  4. Run one of these commands:
find . -type f | wc -l

Counts files in the folder and its subfolders.

find . -type d | wc -l

Counts folders, including the starting folder itself.

find . -maxdepth 1 -type f | wc -l

Counts files directly inside the folder, without counting files in nested folders.

The commands only read directory information. They do not rename, move, modify, or delete anything.

Open Terminal and Choose the Directory

Terminal starts in your home folder, but you can switch to any directory before counting its contents.

  1. Open Finder.
  2. Locate the folder you want to inspect.
  3. Open Terminal from Applications > Utilities.
  4. Type cd followed by a space, but do not press Return yet.
  5. Drag the folder from Finder into the Terminal window.
  6. Press Return.

The cd command means “change directory.” Dragging the folder into Terminal adds its full path automatically, which helps avoid typing spaces or special characters incorrectly.

To confirm your current location, run:

pwd

pwd prints the path of the current directory. To see the visible items there, run:

ls

These commands only display information. They do not change the folder or its contents.

Count All Files in a Directory Tree

A directory tree means the starting folder plus any folders nested inside it. Use this command:

find . -type f | wc -l

What it does:

  • find . searches from the current directory.
  • -type f limits the results to regular files.
  • | sends the results to another command.
  • wc -l counts the number of lines, with each matching path appearing on its own line.

The command includes files in subfolders at every depth. It also counts hidden files, because find does not ignore names that begin with a period.

Terminal showing the command find . -type f | wc -l
Run this command in Terminal for this step.
find . -type f | wc -l

If the command returns 0, no regular files were found in that directory tree. A number larger than zero is the total number of matching files, not the total size of those files.

If you need to investigate storage usage rather than item count, use a disk-usage command or see this guide to finding the largest files and folders in Terminal.

Count All Folders in a Directory Tree

To count directories in the current directory and every nested directory, run:

find . -type d | wc -l

The starting directory is included in this count. For example, if the current folder contains two subfolders, the result is three: the current folder plus the two subfolders.

If you want to count only folders below the starting directory, subtract one from the result mentally, or use this command:

find . -mindepth 1 -type d | wc -l

Here, -mindepth 1 excludes the starting directory itself. The command still includes all nested directories.

The command does not change any files or folders. It only searches their paths and counts the results.

Count Files and Folders Directly Inside One Directory

Sometimes you want to exclude nested content and count only the immediate contents of a folder. Use -maxdepth 1.

To count files directly inside the current directory:

find . -maxdepth 1 -type f | wc -l

To count folders directly inside the current directory:

find . -maxdepth 1 -mindepth 1 -type d | wc -l

The first command includes only regular files at the current level. The second command counts only immediate subdirectories and excludes the starting directory itself.

This distinction is important. For example, a project folder might contain several subfolders, each with many files. A recursive command counts every file in the entire project, while -maxdepth 1 counts only files placed directly in the project’s top-level folder.

Terminal showing the command find . -type d | wc -l
Run this command in Terminal for this step.
find . -maxdepth 1 -type f | wc -l

Count Visible Items With ls

For a quick count of visible entries directly inside the current directory, you can use:

ls -1 | wc -l

ls -1 displays one visible item per line, and wc -l counts those lines. This method does not search subfolders.

It also has important limitations:

  • Hidden files and folders are excluded.
  • The result includes both files and folders together.
  • The count applies only to the current directory.
  • Unusual filenames can make output-based counting less suitable than find.

To include hidden entries in a nonrecursive count, use:

ls -1A | wc -l

The -A option includes hidden entries except the special . and .. directory references. However, for a precise file-only or folder-only count, find is generally clearer:

find . -maxdepth 1 -type f | wc -l
find . -maxdepth 1 -mindepth 1 -type d | wc -l

Count Files by Extension

You can count files with a particular extension by adding -name to find. For example, this command counts Markdown files in the current directory and its subdirectories:

find . -type f -name '*.md' | wc -l

The pattern is enclosed in single quotation marks so the shell passes the asterisk to find instead of expanding it before the command runs.

For a case-insensitive extension match, use -iname:

find . -type f -iname '*.jpg' | wc -l

This matches names such as .jpg and .JPG. Change the pattern to the extension you need, such as *.pdf, *.png, or *.txt.

To count only matching files directly inside the current directory, combine the filter with -maxdepth:

find . -maxdepth 1 -type f -iname '*.pdf' | wc -l

These commands are read-only. They do not alter matching files.

Count Files in a Specific Folder Without Changing Directories

You can provide a path directly to find instead of using cd. For example:

find ~/Downloads -type f | wc -l

This counts regular files in the Downloads folder and all of its subfolders.

For a folder whose name contains spaces, enclose the path in quotes:

find "$HOME/My Folder" -type f | wc -l

$HOME represents your home folder. Quoting the complete path keeps spaces and other shell characters from being interpreted incorrectly.

You can also drag a folder into Terminal after typing find, then add the rest of the command:

find 

After dragging the folder, type:

 -type f | wc -l

Then press Return. Dragging the folder supplies its path without requiring you to type it manually.

Count Items When macOS Requests Permission

Some locations may be protected by macOS privacy controls. Terminal might display a “Permission denied” message or return an incomplete count when it cannot read part of a directory.

First, try counting a folder you own, such as a folder inside your home folder. If you need to inspect a protected location, review the results carefully before granting access.

To check Terminal’s privacy access:

  1. Open Apple menu > System Settings.
  2. Select Privacy & Security.
  3. Review relevant categories, such as Files and Folders or Full Disk Access.
  4. If you change access, follow the on-screen instructions and reopen Terminal if macOS requests it.

Granting broader access can expose more of your files to the application, so enable it only when necessary. If you later want to reverse that change, return to the same privacy setting and remove Terminal’s access. The exact available controls can vary by macOS configuration.

Terminal showing the command find . -type f | wc -l
Run this command in Terminal for this step.
find ~/Downloads -maxdepth 1 -type f | wc -l

Understand Hidden Files and Special Items

find . -type f includes hidden regular files. A hidden file is simply a file whose name begins with a period, such as a configuration file. These files may not appear in Finder’s normal view, but they are still included in the find result.

The -type f filter counts regular files only. It does not count folders, symbolic links, devices, or other filesystem object types as regular files.

To count symbolic links separately, use:

find . -type l | wc -l

A symbolic link is a filesystem reference that points to another file or folder. Counting links separately can help prevent confusion when a directory contains shortcuts or references rather than additional copies of data.

If you need to examine file metadata instead of simply counting paths, you can also check extended attributes on Mac files using Terminal.

Troubleshooting

The count is higher than expected

You may have used a recursive command, which includes files in every subfolder. To count only files directly inside the current directory, use:

find . -maxdepth 1 -type f | wc -l

Hidden files are also included by find. If you used ls -1, the opposite may be true: hidden entries were excluded.

The count is zero

Confirm that Terminal is in the intended directory:

pwd

Then list its visible contents:

ls

If the path is wrong, use cd again or provide the folder path directly to find.

“Permission denied” appears

The command may be unable to read one or more protected folders. Use a directory inside your home folder first. If you need access to another location, review System Settings > Privacy & Security and avoid granting broader access unless it is necessary.

A permission message means the result may not represent every item in the requested tree.

The command seems stuck

A recursive search can take time in a directory containing many files or mounted storage. Press Control-C to stop the running command. This interrupts the search; it does not delete or modify the files already scanned.

Then try a narrower path or a nonrecursive command:

find . -maxdepth 1 -type f | wc -l

The number includes folders you did not expect

Use -type f for files or -type d for directories. If you use ls -1, both types are included together.

You need to compare counts later

Save a count to a text file in a location where you have write access:

find . -type f | wc -l > file-count.txt

This creates or replaces file-count.txt in the current directory. Because this command changes the directory by creating a file, check the path with pwd first.

To remove the report afterward, use Finder or this command only if you are certain the path is correct:

rm file-count.txt

Warning: rm permanently removes the specified file and does not move it to the Trash. Do not run it if the filename or current directory is uncertain. A safer recovery path is to leave the report in place or delete it from Finder, where you can typically review the item before moving it to the Trash.

For more detailed comparisons between directory contents, see how to compare two files or folders using Terminal.

Frequently Asked Questions

Does find . -type f | wc -l count files in subfolders?

Yes. It searches recursively from the current directory, so the result includes regular files in the starting directory and all nested folders.

How do I count only the files in the current folder?

Run:

find . -maxdepth 1 -type f | wc -l

This excludes files inside subfolders.

Does Terminal count hidden files?

The find commands in this guide include hidden files. ls -1 does not include hidden entries, while ls -1A does.

How do I count folders but not the starting folder?

Run:

find . -mindepth 1 -type d | wc -l

This counts directories below the current directory and excludes the current directory itself.

Can I count a folder without opening it in Finder?

Yes. Provide its path directly:

find ~/Downloads -type f | wc -l

You do not need to change directories first.

Does counting files change anything?

The counting commands are read-only. They search directory entries and print a number. Commands that redirect output to a file, such as > file-count.txt, do change the directory by creating or replacing that report file.

Final Thoughts

For most file-counting tasks, use find with a type filter and wc -l:

find . -type f | wc -l

Use -type d for folders and -maxdepth 1 when you want only the directory’s immediate contents. Check the current path with pwd, especially before using any command that creates or removes a file.