shell 统计项目代码行数

Posted by wxianfeng Tue, 16 Nov 2010 17:52:00 GMT

你知道你目前的项目代码有多少行吗?有多少个文件吗?有多少个文件夹吗?。。。^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

This entry was posted on Tue, 16 Nov 2010 17:52:00 GMT and Posted in . You can follow any any response to this entry through the Atom feed. You can leave a comment or a trackback from your own site.

Tags


Trackbacks

Use the following link to trackback from your own site:
http://wxianfeng.com/trackbacks?article_id=97

Comments

  1. Avatar
    indcreateforum.url.ph almost {{count}} years ago:

    I’m curiouus to find out what blog platform you’re using?

    I’m having some small security problems with my latest website
    and I would like to find something more risk-free.
    Do you hafe any suggestions?

  2. Avatar
    Passenger ships Ancona Patras about {{count}} years ago:

    It’svery effortless to find out anyy topic on web as compared to textbooks, as I found tthis article at this web
    site.

  3. Avatar
    Jacki almost {{count}} years ago:

    My developer is trying to persuade me to move to
    .net from PHP. I have always disliked the idea because of
    the expenses. But he’s tryiong none the less.

    I’ve been using WordPress on a number of websites for
    about a year and am worried about switching to another platform.
    I have heard very good things about blogengine.net.
    Is there a way I can transfer all my wordpress
    posts into it? Any help would be greatly appreciated!

  4. Avatar
    Κέρκυρα Ανκόνα almost {{count}} years ago:

    Excellent blog! Do you have any recommendations for aspiring writers?

    I’m hoping to start my own site soon but I’m a little lost on everything.
    Wuld you advise starting with a free platform like Wordpress or ggo for a paid option?There are so many options out there tht I’m totally overwhelmed ..
    Any suggestions? Thank you!

  5. Avatar
    Κέρκυρα Ανκόνα almost {{count}} years ago:

    Excellent blog! Do you have any recommendations
    for aspiring writers? I’m hoping too start my own site soon buut I’m a little losdt on everything.
    Would you advise starting with a frse platform like Wordpress or ggo for
    a paikd option? There are so many options out there that I’m totally overwhelmed ..

    Any suggestions? Thank you!

  6. Avatar
    paketo diakopon gia Romi almost {{count}} years ago:

    I’ve been exploring for a little bit for any high quality
    articles or weblog posts in this kind of space . Exploring
    inn Yahoo I eventually stumbled upon this site. Studying this information So i’m satisfied to show that I’ve an incrediblky excellent uncanny
    feeling I cawme upon exactly what I needed. I most certainly will make sure to
    don?t puut out off your mind tthis webb site and give itt a glance on a
    continuing basis.

  7. Avatar
    paketo diakopon gia Romi almost {{count}} years ago:

    I’ve been exploring for a little bit ffor any high quality articles or weblog
    posts iin this kind of space . Exploring in Yahoo I eventually stumbled upon this site.
    Studying this information So i’m satisfied to show that I’ve an incredibly excellent uncanny feeling I came upin exactly what I
    needed. I most certainly will make sure too
    don?t put out of your mind this weeb site and give it a
    glance on a continuing basis.

  8. Avatar
    Φθηνά καράβια almost {{count}} years ago:

    My relatives all the time say that I am wasting my time here at
    net, but I know I am getting know-how every day by reading
    such good posts.

  9. Avatar
    Φθηνά καράβια almost {{count}} years ago:

    My relatives all the time say that I am wasting my time here at net, but I know I am getting know-how every day
    by reading such gokd posts.

  10. I’m extremely inspired with your writing skills and also with thee layyout to your blog.
    Is this a pid subject matter or did you modify it yourself?

    Either way keep up tthe excellent quality writing, it is
    uncommon to see a nice blog like this one today..

  11. I’m extremely inspired with yojr writing skills and
    also with the layout to your blog. Is this
    a paid sjbject matter or did you modify it yourself?
    Either way keep up the excellent quality writing,
    it is uncommon to see a nice blog like this one today..

  12. Avatar
    dispora.sumselprov.go.id almost {{count}} years ago:

    If you want to grow your knowledge only keep visiting this web page
    and be updated with the most recent information posted here.

  13. Avatar
    dispora.sumselprov.go.id almost {{count}} years ago:

    If you want to grow your knowledge only keep visiting this web page and be updated with the most recent information posted here.

  14. Hi there, its pleasant post about media print, we all be awqre oof media is a
    fantastic source of facts.

  15. Hi there, its plasant post about media print, we all be aware of
    media is a fantastic source of facts.

  16. Avatar
    Φθηνά ferries almost {{count}} years ago:

    I do not know whether it’s just me or if perhaaps everybody
    else experiencing iesues with your website. It appears like some of the text on your content are running off the screen. Can someone
    else please provide feedback andd let me know
    if this is happening to them too? This might be a
    issue with my internet browser because I’ve had this happen before.

    Thanks

  17. Avatar
    Φθηνά ferries almost {{count}} years ago:

    I do not know whether it’s just me or if perhaps everybody else experiencing issues with your website.
    It appears like some of the text on your conteht are running off
    the screen. Can someone elsee please provide feedback and
    let me know if this is happening to them too? This might be a issue with mmy inteernet browser because I’ve had this hazppen before.
    Thanks

  18. Avatar
    Passenger ships Bari Patras about {{count}} years ago:

    Do you have a spam probleem on this blog

  19. Avatar
    Passenger ships Bari Patras about {{count}} years ago:

    Do you have a spam problem on thi blog

Leave a comment