1 概述
作为程序员,你是如何统计自己所写代码行数呢?下面介绍两种统计方式。
2 通过git命令
如果代码放在git仓库中可以使用这种方式。
2.1 统计所有文件
$ git ls-files | xargs wc -l
41 .gitignore
23 AskPass.pro
60 README.en.md
61 README.md
12 doc/sequence_askpass.puml
12 include/crypto/config.h
17 include/crypto/cryptorsa.h
56 include/crypto/priKey.h
15 lib/libCrypto.a
14 main.cpp
52 passwordclient.cpp
24 passwordclient.h
387 total
如上所示:
- 代码行数387
2.2 统计指定文件类型
$ git ls-files '*.h' | xargs wc -l
12 include/crypto/config.h
17 include/crypto/cryptorsa.h
56 include/crypto/priKey.h
24 passwordclient.h
109 total
$ git ls-files '*.cpp' | xargs wc -l
14 main.cpp
52 passwordclient.cpp
66 total
3 通过find命令
3.1 统计所有文件
$ find . -type f -exec wc -l {} +
51 ./.vscode/settings.json
48 ./doc/C++命令行系统(一).md
92 ./doc/C++命令行系统(三).md
464 ./doc/C++命令行系统(二).md
53 ./inc/cppcmd.h
12 ./Makefile
37 ./mkfiles/exe.mk
40 ./mkfiles/lib.mk
371 ./src/cmdhelper.h
19 ./src/cmdio.cpp
13 ./src/cmdio.h
91 ./src/cppcmd.cpp
15 ./src/Makefile
22 ./test/cmdtest.cpp
242 ./test/cmdtest.h
424 ./test/inc/cpptest/cpptest-assert.h
106 ./test/inc/cpptest/cpptest-collectoroutput.h
112 ./test/inc/cpptest/cpptest-compileroutput.h
64 ./test/inc/cpptest/cpptest-htmloutput.h
152 ./test/inc/cpptest/cpptest-output.h
67 ./test/inc/cpptest/cpptest-source.h
140 ./test/inc/cpptest/cpptest-suite.h
88 ./test/inc/cpptest/cpptest-textoutput.h
66 ./test/inc/cpptest/cpptest-time.h
42 ./test/inc/cpptest/cpptest.h
318 ./test/lib/libcpptest.a
17 ./test/Makefile
25 ./test/test.cpp
3191 total
3.2 统计指定文件类型
$ find . -name '*.h' -exec wc -l {} +
53 ./inc/cppcmd.h
371 ./src/cmdhelper.h
13 ./src/cmdio.h
242 ./test/cmdtest.h
424 ./test/inc/cpptest/cpptest-assert.h
106 ./test/inc/cpptest/cpptest-collectoroutput.h
112 ./test/inc/cpptest/cpptest-compileroutput.h
64 ./test/inc/cpptest/cpptest-htmloutput.h
152 ./test/inc/cpptest/cpptest-output.h
67 ./test/inc/cpptest/cpptest-source.h
140 ./test/inc/cpptest/cpptest-suite.h
88 ./test/inc/cpptest/cpptest-textoutput.h
66 ./test/inc/cpptest/cpptest-time.h
42 ./test/inc/cpptest/cpptest.h
1940 total
$ find . -name '*.cpp' -exec wc -l {} +
19 ./src/cmdio.cpp
91 ./src/cppcmd.cpp
22 ./test/cmdtest.cpp
25 ./test/test.cpp
157 total