日期:2014-05-16  浏览次数:20735 次

[转]LINUX下统计代码行数

我们编程时常常想统计一下自己写过多少行代码了,这时候该怎么办呢?
虽然Vim等编辑器中有代码行数显示,但是不能一个个打开然后加起来吧?
这个时候需要用到wc这个工具,呵呵,别看名字不怎么样,功能可是很强大的哦。
用法:

当前目录下:
wc -l *.c *.h

当前目录及子目录:
find . -name *.c |xargs wc -l
find . -name *.cpp | xargs wc -l
find . -name *.h |xargs wc -l

转自:http://hi.baidu.com/ivenvd/blog/item/abe8cbbf2652690d18d81fef.html




统计文件行数(单个文件):

wc -l file

例如:

homer@ubuntu:~/workspace/android/game$ wc -l LGameAndroid2DActivity.java
906 LGameAndroid2DActivity.java



统计目录所有文件行数(全部目录):

find . -name *.java | xargs wc -l

例如:

homer@ubuntu:~/workspace/android$ find . -name *.java | xargs wc -l
    817 ./game/core/LHandler.java
    140 ./game/core/LFlicker.java
    ...
    515 ./game/utils/collection/ArrayMap.java
    162 ./game/utils/CollisionUtils.java
    178 ./game/utils/NumberUtils.java
  68753 total



统计目录并按行数排序(按行大小排序):

find . -name *.java | xargs wc -l | sort -n

homer@ubuntu:~/workspace/android$ find . -name *.java | xargs wc -l | sort -n
     25 ./game/action/sprite/Collidable.java
     26 ./game/core/graphics/component/CollisionQuery.java
     27 ./game/core/graphics/filter/ImageFilter.java
     28 ./game/LMode.java
     ...
   1467 ./game/core/geom/Path2D.java
   1919 ./game/core/graphics/Screen.java
   2417 ./game/core/graphics/device/LGraphics.java
   3050 ./game/core/geom/AffineTransform.java
  68753 total



统计目录并按行数排序(按行文件名排序):

find . -name *.java | xargs wc -l | sort -k2

homer@ubuntu:~/workspace/android$ find . -name *.java | xargs wc -l | sort -k2
    210 ./game/action/ActionControl.java
    116 ./game/action/ActionEvent.java
     34 ./game/action/ActionListener.java
     ....
    178 ./game/utils/NumberUtils.java
    342 ./game/utils/RecordStoreUtils.java
     58 ./game/utils/ScreenUtils.java
    650 ./game/utils/StringUtils.java
  68753 total