How to count the number of files in a directory and the commands for the total number of lines

Source: Internet
Author: User

How to count the number of files in a directory and the commands for the total number of lines under Linux

The total number of files that know the specified suffix name command:

Find. -name "*.cpp" | Wc-l find

.-maxdepth 1-name "*.sh" |wc-l
Know the total number of lines of code in one directory and the number of individual file rows:

Find. -name "*.h" | Xargs wc-l find

.-name "*.C" | xargs grep "^." | wc-l//Excluding blank lines wc-l ' find.-name ' *.c '//Note the Find

content is ~ below the character No, it's wrapped.

Number of files in Linux statistics folder

The first method:

Ls-l|grep "^-" |wc-l

Ls-l: Long list output The file information in this directory (note the file here, different from the general file, may be directories, links, device files, etc.). If the Ls-lr|grep "^-" |wc-l can be counted together with the files in the subdirectory.

grep ^-: This will be a long list of output information filter part, only the general file, if only keep the directory is ^d.

Wc-l: Statistics output The number of lines of information, because has filtered only the general file, so the statistical result is the number of general file information, and because a line of information corresponding to a file, so that is the number of files.

The second method:

Find./-type F|wc-l
Because the default find will go to the subdirectory lookup, if you only want to find the files in the current directory, use finding./-maxdepth 1-type f|wc-l.

It should be explained that the second method is much faster than the first method, especially when subdirectories are counted.


the number of statistical directories and their subdirectory files under Linux system

To view the number of files in a directory:

Ls-l |grep "^-" |wc-l find
./company-type F | wc-l  

View the number of files in a directory, including subdirectories:

Ls-lr|grep "^-" |wc-l
View the number of directories under a folder, including those in subdirectories:

Ls-lr|grep "^d" |wc-l





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.