你知道你目前的项目代码有多少行吗?有多少个文件吗?有多少个文件夹吗?。。。^o(∩∩)o…哈哈,这个很简单,一句shell就搞定了,效率还非常之高:

1,统计你项目的.rb 结尾的代码行数,去掉空格

find . -type f -name "*.rb" | xargs cat | grep -v ^$ | wc -l

当然你还可以添加去掉注释

2,统计项目文件所有行数,包括空行

find . -type f  | xargs cat | wc -l

3,统计项目有多少个文件

find . -type f  | wc -l

4,统计项目有多少个文件夹

find . -type d  | wc -l

5,找出项目下.rb 结尾的文件行数最大的一个(由小到大排序)

find . -type f -name "*.rb"  | xargs wc -l | sort -n

6,自由发挥

总之find 命令结合 xargs 很强大的说

这些都是基本的用法,例如上面想得到tar.gz 等压缩包的内容就难了,那么可以借助cloc(perl库)得到更详细的信息,各种文件类型的行数,注释,空行等等信息:

ubuntu 安装:

sudo apt-get install cloc

使用:

wxianfeng@ubuntu:/usr/local/system/projects/redmine_1_0_3$ cloc .
    6455 text files.
    3119 unique files.                                          
    1035 files ignored.

http://cloc.sourceforge.net v 1.09  T=13.0 s (204.8 files/s, 25293.4 lines/s)
-------------------------------------------------------------------------------
Language          files     blank   comment      code    scale   3rd gen. equiv
-------------------------------------------------------------------------------
Ruby               2076     33371     44800    178357 x   4.20 =      749099.40
YAML                197      1299       691     44512 x   0.90 =       40060.80
Javascript          105      1963      2111     12461 x   1.48 =       18442.28
Ruby HTML           241       716        29      4862 x   4.00 =       19448.00
CSS                  22       366       102      1889 x   1.00 =        1889.00
HTML                 15       120         3       627 x   1.90 =        1191.30
Perl                  2        80        98       233 x   4.00 =         932.00
SQL                   3        17         2        96 x   2.29 =         219.84
XML                   1         0         0         9 x   1.90 =          17.10
-------------------------------------------------------------------------------
SUM:               2662     37932     47836    243046 x   3.42 =      831299.72
-------------------------------------------------------------------------------

另外 rails 中已经封装了一个rake 任务,用来统计的,瞧瞧:

wxianfeng@ubuntu:/usr/local/system/projects/wxianfeng_com$ rake stats
(in /usr/local/system/projects/wxianfeng_com)
+----------------------+-------+-------+---------+---------+-----+-------+
| Name                 | Lines |   LOC | Classes | Methods | M/C | LOC/M |
+----------------------+-------+-------+---------+---------+-----+-------+
| Controllers          |  2422 |  1978 |      36 |     215 |   5 |     7 |
| Helpers              |   858 |   712 |       0 |     103 |   0 |     4 |
| Models               |  3360 |  2661 |      55 |     447 |   8 |     3 |
| Libraries            |  1203 |   884 |      30 |     143 |   4 |     4 |
| APIs                 |   436 |   353 |      17 |      23 |   1 |    13 |
| Functional tests     |    16 |    12 |       2 |       0 |   0 |     0 |
| Unit tests           |    16 |    12 |       3 |       0 |   0 |     0 |
| Model specs          |  2094 |  1680 |       1 |      24 |  24 |    68 |
| View specs           |   249 |   200 |       0 |      12 |   0 |    14 |
| Controller specs     |  3507 |  2734 |       2 |      37 |  18 |    71 |
| Helper specs         |    53 |    39 |       0 |       0 |   0 |     0 |
| Library specs        |    15 |    11 |       0 |       0 |   0 |     0 |
+----------------------+-------+-------+---------+---------+-----+-------+
| Total                | 14229 | 11276 |     146 |    1004 |   6 |     9 |
+----------------------+-------+-------+---------+---------+-----+-------+
  Code LOC: 6588     Test LOC: 4688     Code to Test Ratio: 1:0.7

see:

http://bbs.chinaunix.net/viewthread.php?tid=1665204
http://garfileo.is-programmer.com/2010/6/11/lines-counting-using-cloc.18828.html

posted on 2013-05-28 18:04  andy071001  阅读(442)  评论(0编辑  收藏  举报