Home  >  Article  >  Operation and Maintenance  >  How to count the number of files in a folder or folder under Linux

How to count the number of files in a folder or folder under Linux

巴扎黑
巴扎黑Original
2017-08-21 15:21:392312browse

Count the number of files under a certain folder

ls -l |grep "^-"|wc -l

Count the number of directories under a certain folder

ls -l |grep "^dd"|wc -l

Counts the number of files in a folder, including those in subfolders

ls -lR|grep "^- "|wc -l

If you count all the js files in the /home/han directory (including subdirectories):

ls -lR /home/han|grep js|wc -l Or ls -l "/home/han"|grep "js"|wc -l

Count the number of directories in the folder, including

ls -lR| grep "^d"|wc -l

Explanation:

ls -lR

Long list output file information in this directory (R represents subdirectory, pay attention to the files here, Different from ordinary files, they may be directories, links, device files, etc.)

grep "^-"

Here, part of the long list output information will be filtered, and only general files will be retained. If only The directory is ^d

wc -l

The number of lines of statistical output information has been filtered to only general files, so the statistical result is the number of lines of general file information, and because one line of information Corresponds to one file, so it is the number of files.

========================================

If you only view the folder

ls -d can only display one.

find -type d You can see the subfolder

ls -lF |grep / or ls - l |grep '^d' only looks at the folders in the current directory, excluding the folders below

The above is the detailed content of How to count the number of files in a folder or folder under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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