CLOC -- Count Lines of Code 可能大家都知道用 ` wc -l` 命令进行代码行数统计,但是它会将代码中的注释、空行所占用的文本行都统计在内。如果想查看一个 tar 包或一个项目目录中“实际”的代码行数并且不愿意自己去写一个脚本来做此类工作,那么可以考虑使用 cloc。 cloc 是一个 perl 脚本,它可以统计很多种编程语言的代码文件中的空行、注释以及实际的代 码行数。 下载最新版的color 下载地址:https: //fossies .org /linux/privat/cloc-1 .64. tar .gz /cloc-1 .64 /cloc 百度云盘下载地址:https: //pan .baidu.com /s/1o7B1vyq 安装cloc (mac环境下直接使用brew install cloc安装) [root@huanqiu_web1 ~] # wget https://fossies.org/linux/privat/cloc-1.72.tar.gz [root@huanqiu_web1 ~] # tar -zvxf cloc-1.72.tar.gz [root@huanqiu_web1 ~] # cd cloc-1.72/ [root@huanqiu_web1 cloc-1.72] # make install [root@huanqiu_web1 cloc-1.72] # ls cloc LICENSE README.md sqlite_formatter tests Unix 然后就可以直接使用cloc命令统计代码行数了。 比如统计 /home/www/main 下的代码行数 [root@huanqiu_web1 cloc-1.72] # ./cloc /home/www/main 8098 text files. 3770 unique files. Complex regular subexpression recursion limit (32766) exceeded at . /cloc line 9262. Complex regular subexpression recursion limit (32766) exceeded at . /cloc line 9262. Complex regular subexpression recursion limit (32766) exceeded at . /cloc line 9262. 4671 files ignored. github.com /AlDanial/cloc v 1.72 T=39.32 s (89.5 files /s , 23469.5 lines /s ) ------------------------------------------------------------------------------- Language files blank comment code ------------------------------------------------------------------------------- PHP 2320 51059 127551 378887 JavaScript 520 23226 23625 156244 CSS 429 15131 3098 113634 HTML 131 2023 415 14150 SQL 71 326 392 9853 Stylus 1 254 6 931 ......... JSON 1 0 0 25 make 2 3 0 15 INI 2 18 64 13 Pascal 1 6 119 2 ------------------------------------------------------------------------------- SUM: 3519 92201 155315 675403 ------------------------------------------------------------------------------- 统计压缩包代码行数。比如统计 /usr/local/src 下的mysql-5.6.34. tar .gz压缩包的代码行数 [root@huanqiu_web1 cloc-1.72] # ./cloc /usr/local/src/mysql-5.6.34.tar.gz 14680 text files. 13866 unique files. 10255 files ignored. github.com /AlDanial/cloc v 1.72 T=86.00 s (51.5 files /s , 29614.3 lines /s ) --------------------------------------------------------------------------------------- Language files blank comment code --------------------------------------------------------------------------------------- C++ 1161 188519 214211 944511 C 517 36553 40459 458406 C /C ++ Header 1389 51498 114913 206832 Java 612 13120 19048 54595 Pascal 103 8820 13805 43968 Perl 141 9513 6423 31054 Bourne Shell 142 4970 5671 27393 ........... lex 3 219 102 524 dtrace 9 96 10 456 Prolog 1 9 40 447 Windows Message File 2 4 17 12 Visual Basic 1 0 0 12 --------------------------------------------------------------------------------------- SUM: 4433 318404 422680 1805749 --------------------------------------------------------------------------------------- 对比压缩包代码差异。 [root@huanqiu_web1 cloc-1.72] # ./cloc --diff /usr/local/src/keepalived-1.3.5.tar.gz /usr/local/src/mysql-5.6.34.tar.gz [root@LVS_Backup cloc-1.72] # ./cloc --diff /usr/local/src/keepalived-1.3.5.tar.gz /usr/local/src/mysql-5.6.34.tar.gz 257 text files. 14680 text files. 9565 files ignored. github.com /AlDanial/cloc v 1.72 T=84.00 s (0.0 files /s , 0.0 lines /s ) --------------------------------------------------------------------------------------- Language files blank comment code --------------------------------------------------------------------------------------- CSS same 0 0 0 0 modified 0 0 0 0 added 0 0 0 0 removed 1 37 0 141 m4 ........ --------------------------------------------------------------------------------------- SUM: same 0 0 1 1 modified 1 0 30 36 added 4432 318390 422649 1805712 removed 170 7865 8579 47591 --------------------------------------------------------------------------------------- |