Number of rows in the statistics file

Source: Internet
Author: User

Count the total number of rows in a directory for all normal files


Scenario One: Find. -type f-name "*.C"-exec cat {} \; | Grep-v ' ^$ ' | Wc-l

Explanation: Find. -type f-name "*.c" finds all files ending with. C in the current directory, and can be directly useful if you do not consider file types. -type F can be. -exec Cat {} \;  is to be found in the file using the cat command output; Grep-v ' ^$ ' is not a blank line of content to be counted, if the blank line does not count, it is equivalent to a filter. Wc-l the number of lines of code that the statistic outputs. If you do not need to filter the empty line, you can save Grep-v ' ^$ '

Scenario Two: Find. -type f-exec wc-l {} \; | awk ' {sum+=$1}end{print sum} '

Explanation: Find command with Scheme one. -exec wc-l refers to the number of rows in which the file will be found, which, if output, outputs a similar

Main.c

Head1.h

head1.c

So it's also important to use awk to add the first column, and by Sum+=$1, it's obvious that it's easier to understand the meaning of awk. However, this method does not filter for empty rows.

Scenario Three: Find. -type F | Xargs wc-l
Explanation: The difference between using Xargs and scenario two is that when a matching file is processed using the-EXEC option of the Find command, the Find command passes all matching files to exec execution. However, some systems have limits on the length of commands that can be passed to exec, so that an overflow error occurs several minutes after the find command has been run. The error message is usually either "too long" or "parameter column overflow." This is the use of the Xargs command, especially with the Find command.

The Find command passes the matching file to the Xargs command, and the Xargs command gets only a portion of the file at a time instead of all, unlike the-exec option. This allows it to process the first part of the file, then the next batch, and so on.

For scenario three, the system displays the number of rows per file, and then displays the Total row count:

/main.c
8./abc.c
8./folder/main.c
8./folder/shaoning/test.c
38 Total Dosage

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.