# This example prints line count for all found filestotal=0find /path -type f -name "*.php" | while read FILE; do# You see, use 'grep' instead of 'wc'! for properly countingcount=$(grep -c ^ < "$FILE")echo "$FILE has $count lines"let total=total+count #in bash, you can convert this for another shelldoneecho TOTAL LINES COUNTED: $total
Sebastian Bergmann编写了一个名为PHPLOC的工具,它可以满足您的需求,并在此基础上为您提供项目复杂性的概述。这是其报告的一个示例:
SizeLines of Code (LOC) 29047Comment Lines of Code (CLOC) 14022 (48.27%)Non-Comment Lines of Code (NCLOC) 15025 (51.73%)Logical Lines of Code (LLOC) 3484 (11.99%)Classes 3314 (95.12%)Average Class Length 29Average Method Length 4Functions 153 (4.39%)Average Function Length 1Not in classes or functions 17 (0.49%)
ComplexityCyclomatic Complexity / LLOC 0.51Cyclomatic Complexity / Number of Methods 3.37
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.
find . -name '*.php' -type f -exec wc -l {} \;# faster, but includes total at end if there are multiple filesfind . -name '*.php' -type f -exec wc -l {} +
每个文件中的行,按文件路径排序
find . -name '*.php' -type f | sort | xargs -L1 wc -l# for files with spaces or newlines, use the non-standard sort -zfind . -name '*.php' -type f -print0 | sort -z | xargs -0 -L1 wc -l
每个文件中的行,按行数排序,降序
find . -name '*.php' -type f -exec wc -l {} \; | sort -nr# faster, but includes total at end if there are multiple filesfind . -name '*.php' -type f -exec wc -l {} + | sort -nr
#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;}