方案一:

find . -type f -name “*.c” -exec cat {} ; | grep -v ‘^$’ | wc -l

find -type f -name “*.c” 查找当前目录下所有以.c结尾的文件,如果不考虑文件类型,可以直接实用find . -type f即可。
-exec cat {} ; 是将找到的文件采用cat命令输出;

方案二:

find . -type f -exec wc -l {} ; | awk ‘{sum+=$1}END{print sum}’

解释:find命令同方案一。
-exec wc -l 是指将找到的文件依次计算其中的行数,此时如果输出的话,会输出类似于
22 main.c
43 head1.h
67 head1.c
所以还要采用awk将第一列进行相加,由sum+=$1,很明显,比较容易理解awk的含义。但此种方法没有对空行进行过滤。

方案三:

find . -type f | xargs wc -l

解释:使用xargs与方案二的差异在于在使用find命令的-exec选项处理匹配到的文件时, find命令将所有匹配到的文件一起传递给exec执行。但有些系统对能够传递给exec的命令长度有限制,这样在find命令运行几分钟之后,就会出现 溢出错误。错误信息通常是“参数列太长”或“参数列溢出”。这就是xargs命令的用处所在,特别是与find命令一起使用。
find命令把匹配到的文件传递给xargs命令,而xargs命令每次只获取一部分文件而不是全部,不像-exec选项那样。这样它可以先处理最先获取的一部分文件,然后是下一批,并如此继续下去。

对于方案三,系统会显示每个文件的行数,然后显示总行数:
14 ./main.c
8 ./abc.c
8 ./folder/main.c
8 ./folder/shaoning/test.c
38 总用量


11 条评论

62bet · 2025-12-19 20:03

Just tried out 62bet; it’s alright. Offers a few decent options and easy setup. Pretty straightforward. It’s a decent choice if you’re looking for something simple. Give it a try: 62bet

688betbaixar · 2025-12-24 10:44

Need to baixar 688betbaixar on your phone? Its the best way place your bet, download and give it a go if you want to be able to place bets on the go!Check it out: 688betbaixar

ph22login · 2026-01-04 07:23

Interesting analysis! Seeing patterns in numbers is fascinating, and platforms like ph22 login app make accessing those games-slots, lottery, etc.-super convenient with quick deposits via GCash. Definitely adds to the fun!

91phcom · 2026-01-13 14:20

https://www.91phcom.net I am thanksful for this post!

balato88 · 2026-01-14 20:12

balato88 https://www.balato88u.com

tayawin · 2026-01-15 00:45

tayawin https://www.tayawinch.net

phl789 · 2026-01-15 01:57

phl789 https://www.nphl789.net

peso99 · 2026-01-15 02:26

peso99 https://www.repeso99.net

philbet · 2026-01-15 03:02

philbet https://www.philbetts.net

winehq · 2026-01-25 03:47

winehq https://www.labwinehq.com

AIGO Tools · 2026-03-05 00:28

Excellent comparison of line counting methods! The xargs approach is particularly valuable for handling large codebases efficiently. These command-line techniques demonstrate how Linux tools can be combined for powerful data processing. Reminds me of how AI Girlfriend tools leverage similar efficiency principles in processing user interactions at scale.

回复 tayawin 取消回复

Avatar placeholder

您的邮箱地址不会被公开。 必填项已用 * 标注