How to Find Number of Files in a Directory and Subdirectories

In this guide, we will cover how to display the total number of files in the current working directory or any other directory and its subdirectories on a Linux system.

We will use the find command which is used to search for files in a directory hierarchy together with wc command which prints newline, word, and byte counts for each file, alternatively data read from standard input.

Following are the options that we can use with find command as follows:

  1. -type – specifies the file type to search for, in the case above, the f means find all regular files.
  2. -print – an action to print the absolute path of a file.
  3. -l – this option prints the total number of newlines, which is equals to the total number of absolute file paths output by find command.

The general syntax of find command.

# find . -type f -print | wc -l
$ sudo find . -type f -print | wc -l

Important: Use sudo command to read all files in the specified directory including those in the subdirectories with superuser privileges, in order to avoid “Permission denied” errors as in the screen shot below:

Find Number of Files in Linux
Find Number of Files in Linux

You can see that in the first command above, not all files in the current working directory are read by find command.

The following are extra examples to show total number of regular files in /var/log and /etc directories respectively:

$ sudo find /var/log/ -type f -print | wc -l
$ sudo find /etc/ -type f -print | wc -l

For more examples on Linux find command and wc command go through the following series of articles for additional usage options, tips and related commands:

  1. 35 Useful ‘find’ Command Examples in Linux
  2. How to Find Recent or Today’s Modified Files in Linux
  3. Find Top 10 Directoires and Files Disk Space in Linux
  4. 6 Useful ‘wc’ Command Examples to Count Lines, Words and Characters

That’s all! In case you know of any other method to display the total number of files in a directory and its subdirectories, do share it with us in the comments.

Hey TecMint readers,

Exciting news! Every month, our top blog commenters will have the chance to win fantastic rewards, like free Linux eBooks such as RHCE, RHCSA, LFCS, Learn Linux, and Awk, each worth $20!

Learn more about the contest and stand a chance to win by sharing your thoughts below!

Aaron Kili
Aaron Kili is a Linux and F.O.S.S enthusiast, an upcoming Linux SysAdmin, web developer, and currently a content creator for TecMint who loves working with computers and strongly believes in sharing knowledge.

Each tutorial at TecMint is created by a team of experienced Linux system administrators so that it meets our high-quality standards.

Join the TecMint Weekly Newsletter (More Than 156,129 Linux Enthusiasts Have Subscribed)
Was this article helpful? Please add a comment or buy me a coffee to show your appreciation.

1 Comment

Leave a Reply

Got Something to Say? Join the Discussion...

Thank you for taking the time to share your thoughts with us. We appreciate your decision to leave a comment and value your contribution to the discussion. It's important to note that we moderate all comments in accordance with our comment policy to ensure a respectful and constructive conversation.

Rest assured that your email address will remain private and will not be published or shared with anyone. We prioritize the privacy and security of our users.