If you are using an operating system, you must count the number of files available in a directory as it provides a brief overview of the contents. It allows you to limit storage space and get details of unnecessarily large files. Additionally, as a Bash script user, you can count the number of files to automate specific tasks and limit the number of them.
However, many beginners want to know how to check the number of files. Therefore, we provide several commands on the blog that can help them quickly count the number of files available in a directory.
Let us divide this guide into multiple sections, in which we will explain the different commands to count the number of files in Linux.
1. Wc command
You can use the "wc" command and "ls" to check the number of files in the directory. For example, let's count the number of files available in Downloads.
LS. | WC—L
The "-l" option instructs to count lines instead of words. If you want to count hidden files, use the "-a" option.
LS—a| WC—L
Counting specific types of files is also easy as you just need to run the following command. For example, let's count ". js" files:
ls *.js| WC—L
To count all visible and hidden files in a directory, you can also use the following command:
Found.—Type F|WC—L
Note: The previous command includes hidden files.
2. Tree command
The "tree" command is useful when working with nested subdirectories because the command provides explicit information about the files. Additionally, the tree also shows a summary at the end, including the number of files. If your system does not have the "tree" utility, run the following command:
Sudo apt installation tree (Ubuntu)
sudo dnf install tree (Fedora)
SUDO YUM installation tree (based on RHEL operating system)
Note: By default, the "tree" command is recursive, which means that the output can include all subdirectories.
Tree
Since the previous command does not include hidden files, run the following command to display them:
tree—a
It's all about multiple ways of counting files in a directory. Remember that counting the number of files in a directory can help you perform routine system checks and storage cleanup. You should use the "tree" command to get brief information on a directory.
The above is the detailed content of How to count the number of files in a directory on Linux. For more information, please follow other related articles on the PHP Chinese website!