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.
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:
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:
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:
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:
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:
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:
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:
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:
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:
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:
That's it.