Home  >  Article  >  System Tutorial  >  Number of Linux statistical files: Comprehensive mastery through multiple methods

Number of Linux statistical files: Comprehensive mastery through multiple methods

王林
王林forward
2024-02-09 17:21:22651browse

Have you ever wondered how to quickly count the number of files in a certain directory in a Linux system? As a Linux user, this is a very common need. Whether it is system maintenance or file management, we all need to master this skill. In this article, we’ll introduce you to a number of different ways to achieve this goal.

Count the number of files in the directory

The simplest way to count files in a directory is to use ls to list one file per line and pipe the output to wc to calculate the number:

[root@localhost ~]# ls -1U /etc |wc -l

Executing the above command will display the sum of all files, including directories and symbolic links. The -1 option means to list one file per line, and the -U tells ls not to sort the output, which makes the command execute faster. The ls -1U command does not count hidden files. If you only want to count files and not include directories, use the following command:

[root@localhost ~]# ls -1Up /etc |grep -v /|wc -l
The

-p option forces ls to append the slash (/) directive to the directory. The output is piped to the grep -v command, lines containing slashes are excluded, and the number is counted.
For better control over the files listed, use the find command instead of ls:

[root@localhost ~]# find /etc -maxdepth 1 -type f |wc -l
The

-type f option tells find to list only files (including hidden files), and the -maxdepth 1limits the search to the first level of directories.

Files in the recursive statistics directory

If you want to count the number of files in a directory, including those in subdirectories, you can use the find command:

[root@localhost ~]# find /etc -type f|wc -l

Another command used to count files is tree, which lists the contents of the directory in a tree format:

[root@localhost ~]# yum -y install tree
[root@localhost ~]# tree /root

Linux 统计文件数量:多种方法全面掌握
The bottom of the output content will show how many directories and how many files there are.

Through the introduction and demonstration of this article, I believe you have mastered various methods of counting the number of files in Linux systems and understand their advantages and disadvantages. Whether you are a beginner or an experienced Linux user, these tips can help you process files more efficiently and improve your work efficiency. In the Linux world, counting files is no longer a troublesome task, but an easy skill to master!

The above is the detailed content of Number of Linux statistical files: Comprehensive mastery through multiple methods. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete