我们有一个PHP应用程序,希望统计特定目录及其子目录下的所有代码行。
我们不需要忽视评论,因为我们只是想得到一个粗略的想法。
wc -l *.php
该命令对于给定的目录非常有效,但它忽略了子目录。我想下面的评论可能有用,但它返回了74,这绝对不是这样。。。
find . -name '*.php' | wc -l
重新输入目录中所有文件的正确语法是什么?
我们有一个PHP应用程序,希望统计特定目录及其子目录下的所有代码行。
我们不需要忽视评论,因为我们只是想得到一个粗略的想法。
wc -l *.php
该命令对于给定的目录非常有效,但它忽略了子目录。我想下面的评论可能有用,但它返回了74,这绝对不是这样。。。
find . -name '*.php' | wc -l
重新输入目录中所有文件的正确语法是什么?
当前回答
在类Unix系统上,有一个叫做cloc的工具,它提供代码统计信息。
我在我们的代码库中的一个随机目录中运行,它显示:
59 text files.
56 unique files.
5 files ignored.
http://cloc.sourceforge.net v 1.53 T=0.5 s (108.0 files/s, 50180.0 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
C 36 3060 1431 16359
C/C++ Header 16 689 393 3032
make 1 17 9 54
Teamcenter def 1 10 0 36
-------------------------------------------------------------------------------
SUM: 54 3776 1833 19481
-------------------------------------------------------------------------------
其他回答
您需要一个简单的for循环:
total_count=0
for file in $(find . -name *.php -print)
do
count=$(wc -l $file)
let total_count+=count
done
echo "$total_count"
我使用了从源项目目录启动的内联脚本:
for i in $(find . -type f); do rowline=$(wc -l $i | cut -f1 -d" "); file=$(wc -l $i | cut -f2 -d" "); lines=$((lines + rowline)); echo "Lines["$lines"] " $file "has "$rowline"rows."; done && unset lines
产生此输出的:
Lines[75] ./Db.h has 75rows.
Lines[143] ./Db.cpp has 68rows.
Lines[170] ./main.cpp has 27rows.
Lines[294] ./Sqlite.cpp has 124rows.
Lines[349] ./Sqlite.h has 55rows.
Lines[445] ./Table.cpp has 96rows.
Lines[480] ./DbError.cpp has 35rows.
Lines[521] ./DbError.h has 41rows.
Lines[627] ./QueryResult.cpp has 106rows.
Lines[717] ./QueryResult.h has 90rows.
Lines[828] ./Table.h has 111rows.
使用Z shell(zsh)globs非常简单:
wc -l ./**/*.php
如果您正在使用Bash,则只需升级。绝对没有理由使用Bash。
这里有一个使用旧Python(至少适用于Python2.6)的灵活应用程序,结合了Shizzmo可爱的一行程序。只需在类型列表中填写源文件夹中要计算的文件类型,然后让它飞起来:
#!/usr/bin/python
import subprocess
rcmd = "( find ./ -name '*.%s' -print0 | xargs -0 cat ) | wc -l"
types = ['c','cpp','h','txt']
sum = 0
for el in types:
cmd = rcmd % (el)
p = subprocess.Popen([cmd],stdout=subprocess.PIPE,shell=True)
out = p.stdout.read().strip()
print "*.%s: %s" % (el,out)
sum += int(out)
print "sum: %d" % (sum)
我是这样做的:
以下是lineCount.c文件实现:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int getLinesFromFile(const char*);
int main(int argc, char* argv[]) {
int total_lines = 0;
for(int i = 1; i < argc; ++i) {
total_lines += getLinesFromFile(argv[i]); // *argv is a char*
}
printf("You have a total of %d lines in all your file(s)\n", total_lines);
return 0;
}
int getLinesFromFile(const char* file_name) {
int lines = 0;
FILE* file;
file = fopen(file_name, "r");
char c = ' ';
while((c = getc(file)) != EOF)
if(c == '\n')
++lines;
fclose(file);
return lines;
}
现在打开命令行并键入gcc-lineCount.c,然后键入/a.out*.txt文件。
这将显示目录中以.txt结尾的文件的总行数。