However, many beginners want to know how to check the number of files. So, in the blog, we included multiple commands to count the number of available files in the directory quickly.
How to Count the Number of Files in a Directory on Linux
Let’s divide this guide into multiple parts where we will explain the different commands to count the number of files in Linux.
1. The Wc Command
You can use the “wc” command with “ls” to check the number of files in a directory. For example, let’s count the number of available files in the “Downloads”.
The “-l” option instructs to count the lines rather than words. If you want to count the hidden files, use the “-a” option.
Counting specific types of files is also simple as you only need to run the following command. For example, let’s count the “.js” files:
To count all visible and hidden files in a directory, you can also use the following command:
Note: The pervious command includes the hidden files.
2. The Tree Command
The “tree” command is useful while dealing with the nested subdirectories as this command provides a clear information about your files. Moreover, the “tree” also shows the summary at the end, including the number of files. In case your system doesn’t have the “tree” utility, run the following command:
sudo dnf install tree (Fedora)
sudo yum install tree (RHEL based OS)
Note: By default, the “tree” command is recursive which means that the output can include all subdirectories.
Since the previous command doesn’t include the hidden files, run the following command to display them:
Conclusion
This is all about the multiple methods of counting the files in a directory. Remember that counting the number of files in a directory can be beneficial for you to perform the regular system checks and storage cleanup. You should use the “tree” command for a brief information in the directory.