我有个树莓派,平时都是在办公室当作NAS用,爱好是Python编程,就经常往树莓派备份自己写的代码脚本和文档,想使用Ubuntu统计下自己的代码行数,看了好多的文章,就测试了一下,初学的朋友可以看看。
我在目录下放了一个两个脚本。
root@yert-desktop:/home/Python/test1# ls
Yert0002.python Yert0003.python
1.使用wc的方式:
root@yert-desktop:/home/Python/test1# wc -l *.python # *.python表示所有文件为Python结尾的。
36 Yert0002.python
9 Yert0003.python
45 total
但是适合于在同一个目录内的,如果有文件在子目录下就不能统计到了。
root@yert-desktop:/home/Python/test1# mkdir dd
root@yert-desktop:/home/Python/test1# ls
dd Yert0002.python Yert0003.python
root@yert-desktop:/home/Python/test1# cp *.python /home/Python/test1/dd/
root@yert-desktop:/home/Python/test1# ls dd/
Yert0002.python Yert0003.python
root@yert-desktop:/home/Python/test1# wc -l *.python # 无法统计到子目录dd下文件的代码行数
36 Yert0002.python
9 Yert0003.python
45 total
root@yert-desktop:/home/Python/test1# ls
dd Yert0002.python Yert0003.python
2.使用find的方式:
建议先退出到根目录或者其他目录。
root@yert-desktop:/home/Python# find test1/ -name "*.python" #首先列举出所有文件
test1/Yert0003.python
test1/Yert0002.python
test1/dd/Yert0003.python
test1/dd/Yert0002.python
root@yert-desktop:/home/Python# find test1/ -name "*.python" | wc -l #统计所有文件数量
4
root@yert-desktop:/home/Python# find test1/ -name "*.python" |xargs cat |wc -l #统计所有文件代码行数
90
root@yert-desktop:/home/Python# find test1/ -name "*.python" |xargs cat |grep -v ^$|wc -l
90
#过滤代码中的空行,统计实际代码数量
查考文章:
https://blog.youkuaiyun.com/ubuntulover/article/details/35981865
https://blog.youkuaiyun.com/xiao_yuanjl/article/details/78905160
《rhel7系统管理》