【Linux】常用命令-统计代码行数

公司人员流动大,经常有新的维护任务,交接时喜欢看看新来的模块的代码量,那么问题来了,

如何统计代码行数?

1,最先想到的肯定是 wc。

wc -l *.h

将查看【当前目录】下头文件的代码行数,输出结果如下:

[groot]$wc -l *.h
54 consts.h
60 crc32.h
169 crypt.h
301 ebcdic.h
443 globals.h
39 inflate.h
81 timezone.h
227 ttyio.h
722 unzip.h
3123 unzpriv.h
89 unzvers.h
25 zip.h
5333 total

然后问题来了,子目录呢?

这时需要其他命令的配合:wc -l `find . -name "*.h";find . -name "*.cpp"`

33 ./netware/nlmcfg.h
722 ./unzip.h
136 ./acorn/riscos.h
69 ./acorn/swiven.h
60 ./crc32.h
443 ./globals.h
342 ./msdos/doscfg.h
66 ./aosvs/aosvs.h
57 ./macos/source/helpers.h
64 ./macos/source/pathname.h
72 ./macos/source/macstat.h
258 ./macos/source/maccfg.h

...

2,wc 也可以用来统计文件个数:

find . -name "*.h" | wc -l,一前以后,用途不同了。

3,如果想只统计非空行呢?
思路是先用grep过滤掉空行:find . \( -name "*.h" -o -name "*.c" \) | xargs cat | grep -v ^$ | wc -l。

神奇的命令~

posted @ 2015-05-19 23:20  狂暴的水牛  阅读(1442)  评论(0编辑  收藏  举报