我们有一个PHP应用程序,希望统计特定目录及其子目录下的所有代码行。

我们不需要忽视评论,因为我们只是想得到一个粗略的想法。

wc -l *.php 

该命令对于给定的目录非常有效,但它忽略了子目录。我想下面的评论可能有用,但它返回了74,这绝对不是这样。。。

find . -name '*.php' | wc -l

重新输入目录中所有文件的正确语法是什么?


当前回答

您可以使用名为codel(link)的实用程序。这是一个简单的Python模块,可以用彩色格式计算行数。

安装

pip install codel

用法

要计算C++文件的行数(扩展名为.cpp和.h),请使用:

codel count -e .cpp .h

您也可以忽略一些.gitignore格式的文件/文件夹:

codel count -e .py -i tests/**

它将忽略测试/文件夹中的所有文件。

输出如下所示:

您还可以使用-s标志缩短输出。它将隐藏每个文件的信息,只显示每个扩展名的信息。示例如下:

其他回答

您没有指定有多少个文件或所需的输出。

这可能是您想要的:

find . -name '*.php' | xargs wc -l

我是这样做的:

以下是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结尾的文件的总行数。

lines=0 ; for file in *.cpp *.h ; do lines=$(( $lines + $( wc -l $file | cut -d ' ' -f 1 ) )) ; done ; echo $lines

对于Windows,一个简单快捷的工具是LocMetrics。

我想检查多个文件类型,并懒得手动计算总数。所以我现在用这个来一次性得到总数。

find . -name '*.js' -or -name '*.php' | xargs wc -l | grep 'total'  | awk '{ SUM += $1; print $1} END { print "Total text lines in PHP and JS",SUM }'

7935115318PHP和JS中的文本行总数94669

这允许您链接多个想要过滤的扩展类型。只需将它们添加到-name“*.js”-或-name“*.php”部分,并可能根据您的喜好修改otuput消息。