How to Count Number of Files and Subdirectories inside a Given Directory

The easiest way to count number of files and subdirectories in a directory using a tree command, which is best known for displaying files and directories in tree-like form.

Although you can always enable quotas to restrict disk space and inode usage to avoid user abuse, this command may be useful anyway. By default, the current working directory is assumed if no arguments are given:

$ tree -iLf 1
Sample Output
./teamviewer 11 0 57095 i386

3 directories, 22 files

If you want to view the same information for /var/log, do:

$ tree -iLf 1 /var/log
Sample Output

13 directories, 23 files

Type the command below to view information about files and subdirectories in the directory ISOs.

$ tree -iLf 1 ISOs 
Sample Output

2 directories, 11 files
Count Files and Subdirectories in a Directory

Count Files and Subdirectories in a Directory

Explaining tree options used in the command above:

  1. -i – its a graphical option that enables tree to print out indentation lines
  2. -L – specifies the level of depth of the directory tree to be displayed, which in the case above is 1
  3. -f – makes tree print the full path prefix for every file

As you can view from the image above, after listing all the files and subdirectories, tree shows you the total number of directories and files in the directory you specified.

Suggested Read: Encrypt and Decrypt Files and Directories Using Tar and OpenSSL

You can refer to the tree man page to discover more useful options, some configuration files and environment variables to better understand how it works.


Here, we covered an important tip that can help you use the tree utility in a different way as compared to its traditional use, for displaying files and directories in a tree-like form.

You can can build new tips by using the numerous tree options from the man page. Do you have any useful tip concerning the use of tree? Then share it with millions of Linux users around the world through the feedback form below.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Gabriel Cánepa

Gabriel Cánepa is a GNU/Linux sysadmin and web developer from Villa Mercedes, San Luis, Argentina. He works for a worldwide leading consumer product company and takes great pleasure in using FOSS tools to increase productivity in all areas of his daily work.

Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.

RedHat RHCE and RHCSA Certification Book
Linux Foundation LFCS and LFCE Certification Preparation Guide
The Complete Linux System Administrator Bundle
Become an Ethical Hacker Bonus Bundle

You may also like...

2 Responses

  1. Jonix says:

    The switch -a also gives the dot-files….otherwise the file/directory count _can_ be misleading….

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.