How to count the number of files in a directory on Linux

PHPz
Release: 2024-03-20 17:16:29
forward
1216 people have browsed it

How to count the number of files in a directory on Linux

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.

How to count the number of files in a directory on Linux

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

in conclusion

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!

source:mryunwei.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!