Do you want to find out the number of files (either .txt, .sh, or .jpg) or directories residing in a specific directory on Linux? Then you've come to the right place. In this article, you will learn how to find the number of files and directories in a specific directory.

Counting Number of Files and Directories in a Specific Directory

There are many ways to find or count the number of files and directories in a specific directory, but today we will show you how it's done using the most popular Linux find command, which comes preinstalled in most Linux distributions and can perform this task quickly without consuming much system resources.

I'll use the my_dir directory I created to demonstrate all the examples; you can check all the files and directories it contains in the following tree hierarchy.

content of test directory

So, let's start this article by finding and counting the number of directories in a specific Linux directory.

Finding and Counting the Number of Directories in a Specific Directory

There are two things that one wants to know: first, the number of directories in a specific directory, and second, the number of nested directories (directories inside other directories) in that specific directory.

So, we'll start by determining the number of directories in a specific directory using the following command:

$ find my_dir/* -mindepth 0 -maxdepth 0 -type d

Output:

finding the number of directories in a specific directory

To find out all the directories (including nested directories) in a specific directory, you can use the following command:

$ find my_dir/* -type d

Output:

finding the number of nested directories in a specific directory

Finally, rather than printing them, you can count each occurrence of the directories and print the total sum in the output using the wc command:

$ find my_dir/* -mindepth 0 -maxdepth 0 -type d | wc -l
$ find my_dir/* -type d | wc -l

Output:

counting the number of directories in a specific directory

Finding and Counting the Number of Files in a Specific Directory

Finding and counting the number of files in a specific directory is as straightforward as finding a directory. For example, to find the number of files in a specific directory at the top level, you can use the following command:

$ find my_dir/* -mindepth 0 -maxdepth 0 -type f

Output:

finding the number of files in a specific directory

You can use the following command to find all the files in a specific directory (including files inside other directories):

$ find my_dir/* -type f

Output:

finding the number of nested files in a specific directory

Finally, instead of printing them, you can count each occurrence of the files and print the total sum in the output by using the following command:

$ find my_dir/* -mindepth 0 -maxdepth 0 -type f | wc -l
$ find my_dir/* -type f | wc -l

Output:

counting the number of files in a specific directory

Finding and Counting the Number of Specific Files in a Specific Directory

In the previous example, you learned to count files that include all types of files, but if you are interested only in specific types of files, then you can exclusively specify their extension to the find command.

For example, the next two commands will only search for the text and shell script files in the specific directory at the top-level, separately.

$ find my_dir/* -mindepth 0 -maxdepth 0 -type f -name "*.txt"
$ find my_dir/* -mindepth 0 -maxdepth 0 -type f -name "*.sh"

Output:

finding the number of specific files in a specific directory

To find the number of specific files in all the nested directories, you can use the following command:

$ find my_dir/* -type f -name "*.txt"
$ find my_dir/* -type f -name "*.sh"

Output:

finding the number of nested files with specific type in a specific directory

Lastly, instead of printing them, you can count each occurrence of your specific file and print the total sum in the output by using the following command:

$ find my_dir/* -type f -name "*.txt" | wc -l
$ find my_dir/* -type f -name "*.sh" | wc -l

Output:

counting the number of specific files in a specific directory

In my directory, there are no images, but if you have them and want to find them, you can specify multiple image file extensions, such as .jpg, .png, .gif to the find command.

$ find my_dir/* -type f \( -name "*.png" -o -name "*.jpg" \)

Get a Summary of All the Files and Directories in a Specific Directory

If you simply want to get a summary of the number of files and directories in a specific directory, then you can use the following command:

$ tree my_dir/ | tail -1

Output:

summary of total number of files and directories in a specific directory

That's it.