How to Check the Number of Files in a Selected Directory and Its Subdirectories

On this page
  1. Prerequisites
  2. Command
  3. Example Output
  4. Understanding the Results
  5. Checking a Specific Website Directory
  6. Why Check File Counts?
  7. Summary

If you want to determine which directory contains the largest number of files, you can use a command that counts files and directories within the current location and its immediate subdirectories.

This can be particularly useful when troubleshooting high inode usage, identifying directories containing large numbers of files, or locating applications that generate excessive temporary data.

Prerequisites

You must have SSH access enabled for your hosting account before running the command.

Once connected via SSH, navigate to the directory you want to analyze.

Command

Run the following command in the SSH terminal:

echo "File count for: $(pwd)" ; for d in `find -maxdepth 1 -type d | cut -d\/ -f2 | grep -xv . | sort`; do c=$(find $d | wc -l) ; printf "$c\t\t- $d\n" ; done ; printf "Total: \t\t$(find $(pwd) | wc -l)\n"

Example Output

File count for: /home/user

245         - backups
1342        - cache
5876        - logs
15423       - public_html

Total:      22886

Understanding the Results

The command displays:

  • The current directory being analyzed.
  • The total number of files and subdirectories found within each first-level directory.
  • The overall total count for the current location.

For example:

15423       - public_html

means that the public_html directory contains 15,423 files and subdirectories.

The final line:

Total:      22886

shows the total number of files and directories within the analyzed path.

Checking a Specific Website Directory

To analyze a particular website, first navigate to its directory:

cd ~/domains/example.com/public_html

Then run the command above.

This can help identify:

  • Cache directories containing excessive files.
  • Backup folders consuming inode limits.
  • Log directories that have grown unexpectedly.
  • Applications generating large numbers of temporary files.

Why Check File Counts?

Even when disk space usage is low, hosting accounts may be affected by a large number of files.

Monitoring file counts can help:

  • Improve website performance.
  • Reduce backup times.
  • Prevent inode limit issues.
  • Identify unnecessary files that can be removed.

Summary

To check the number of files and directories within the current location and its subdirectories, connect via SSH and run:

echo "File count for: $(pwd)" ; for d in `find -maxdepth 1 -type d | cut -d\/ -f2 | grep -xv . | sort`; do c=$(find $d | wc -l) ; printf "$c\t\t- $d\n" ; done ; printf "Total: \t\t$(find $(pwd) | wc -l)\n"

The command provides a quick overview of which directories contain the largest number of files, making it easier to identify potential storage and inode usage issues.

Didn't find an answer?

Our support team is here for you 24/7 – write to us and we will help you sort it out.