The method of counting the number of files in the folder in Linux system

Time:2021-1-21

One is that:

Copy code

The code is as follows:

ls -l |grep “^-” | wc -l

explain:

LS – l long list output file information under the directory (note that the file here is different from the general file, which may be directory, link, device file, etc.)
Grep ^ – filter part of the output information of the long list. Only general files are kept. If only the directory is kept, it is ^ D
WC – L counts the number of rows of output information. Because only general files have been filtered, the statistical result is the number of rows of general file information. Because one line of information corresponds to one file, it is also the number of files.

View the number of files in the folder (the number of files in the current directory and subfolders)

Copy code

The code is as follows:

find ./ -type f | wc -l

perhaps

Copy code

The code is as follows:

ls -lR|grep “^-”|wc -l

The other one is that

Copy code

The code is as follows:

ls -alR dir_name | awk ‘{if(NF>2&&substr($1,1,1)!=”d”)print $0}’ |wc -l

It is strange that there is a slight difference in the number of files counted by the two methods. The latter method is slightly more than the former, but the difference is not big, so it can be used as a reference.

Recommended Today

How to Build a Cybersecurity Career

Original text:How to Build a Cybersecurity Career How to build the cause of network security Normative guidelines for building a successful career in the field of information security fromDaniel miesslerstayinformation safetyCreated / updated: December 17, 2019 I’ve been doing itinformation safety(now many people call it network security) it’s been about 20 years, and I’ve spent […]