A set of practical shell commands that may be frequently used in learning # update and learn slowly #

Source: Internet
Author: User

1. Count the total number of rows of all common files in a directory

# Practical scenarios: when designing a graduation project, you need to calculate your ownCodeNumber of rows #

Solution 1:Find.-Type F-name"*. C"-Exec cat {}\; | grep-V'^ $'| WC-l

Explanation: Find. -Type F-name "*. C "find all. if you do not consider the file type, you can directly use find. -Type F. -Exec cat {}\; Is to output the found file using the cat command; grep-V '^ $' indicates that the content is not empty rows for statistics. If it is empty rows, no statistics are collected, it is equivalent to a filter. WC-l counts the number of lines of code output. If you do not need to filter empty rows, you can save grep-V '^ $'

Solution 2:Find.-Type F-exec WC-l {}\; | awk '{sum + = $1} end {print sum }'

Explanation: The find command is the same as solution 1. -Exec WC-L is used to calculate the number of lines in the files that are found in sequence. In this case, the output is similar

22 main. c

43 head1.h

67 head1.c

So we need to use awk to add the first column from Sum + = $1. Obviously, it is easier to understand the meaning of awk. However, this method does not filter empty rows.

Solution 3:Find.-Type F | xargs WC-l
Explanation: The difference between xargs and solution 2 is thatWhen you use the-exec option of the find command to process matched files,
The find command passes all matching files to Exec for execution. However, some systems have limits on the command length that can be passed to exec, so that after the find command is run for a few minutes
Overflow error. The error message is usually "the parameter column is too long" or "parameter column overflow ". This is the use of the xargs command, especially used with the find command.

The find command passes the matching file to the xargs command, while the xargs command only obtains part of the file, not all, at a time, unlike the-exec option. In this way, it can first process the first part of the obtained files, then the next batch, and continue like this.

For solution 3, the system displays the number of rows for each file and the total number of rows:

14./Main. c
8./ABC. c
8./folder/Main. c
8./folder/shaoning/test. c
38 total usage


Thinking questions: (focus on understanding the meaning of xargs)

Command to know the total number of objects with the specified Suffix: (the number of HTML files is obtained)

Find.-Name "*. html" | WC-l

Know the total number of lines of code in a directory and the number of lines in a single file: (the number of lines in each and all HTML files is obtained)

Find.-Name "*. html" | xargs WC-l

Related Article

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.