bash 如何递归计算目录中的所有代码行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1358540/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 18:28:31  来源:igfitidea点击:

How to count all the lines of code in a directory recursively?

bashshell

提问by user77413

We've got a PHP application and want to count all the lines of code under a specific directory and its subdirectories. We don't need to ignore comments, as we're just trying to get a rough idea.

我们有一个 PHP 应用程序,想要计算特定目录及其子目录下的所有代码行数。我们不需要忽略评论,因为我们只是想得到一个粗略的想法。

wc -l *.php 

That command works great within a given directory, but ignores subdirectories. I was thinking this might work, but it is returning 74, which is definitely not the case...

该命令在给定目录中效果很好,但会忽略子目录。我认为这可能有效,但它返回 74,这绝对不是这种情况......

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

What's the correct syntax to feed in all the files?

输入所有文件的正确语法是什么?

回答by Peter Elespuru

Try:

尝试:

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

The SLOCCount toolmay help as well.

SLOCCount 工具也可能有所帮助。

It will give an accurate source lines of code count for whatever hierarchy you point it at, as well as some additional stats.

它将为您指向的任何层次结构提供准确的源代码行数,以及一些额外的统计信息。

Sorted output:

排序输出:

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

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

回答by Shizzmo

For another one-liner:

对于另一个单线:

( find ./ -name '*.php' -print0 | xargs -0 cat ) | wc -l

works on names with spaces, only outputs one number.

适用于带空格的名称,只输出一个数字。

回答by Michael Wild

If using a decently recent version of Bash (or ZSH), it's much simpler:

如果使用最新版本的 Bash(或 ZSH),它会简单得多:

wc -l **/*.php

In the Bash shell this requires the globstaroption to be set, otherwise the **glob-operator is not recursive. To enable this setting, issue

在 Bash shell 中,这需要设置globstar选项,否则**glob-operator 不是递归的。要启用此设置,请发出

shopt -s globstar

To make this permanent, add it to one of the initialization files (~/.bashrc, ~/.bash_profileetc.).

为了使这个永久性的,它添加到初始化文件之一(~/.bashrc~/.bash_profile等等)。

回答by simao

You can use the clocutility which is built for this exact purpose. It reports each the amount of lines in each language, together with how many of them are comments etc. CLOC is available on Linux, Mac and Windows.

您可以使用cloc专为此目的而构建的实用程序。它报告每种语言的行数,以及其中有多少是注释等。CLOC 可用于 Linux、Mac 和 Windows。

Usage and output example:

用法和输出示例:

$ cloc --exclude-lang=DTD,Lua,make,Python .
    2570 text files.
    2200 unique files.                                          
    8654 files ignored.

http://cloc.sourceforge.net v 1.53  T=8.0 s (202.4 files/s, 99198.6 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Javascript                    1506          77848         212000         366495
CSS                             56           9671          20147          87695
HTML                            51           1409            151           7480
XML                              6           3088           1383           6222
-------------------------------------------------------------------------------
SUM:                          1619          92016         233681         467892
-------------------------------------------------------------------------------

回答by Calmarius

On UNIX-like systems, there is a tool called clocwhich provides code statistics.

在类 UNIX 系统上,有一个称为cloc提供代码统计信息的工具。

I ran in on a random directory in our code base it says:

我在我们的代码库中的一个随机目录中运行它说:

      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
-------------------------------------------------------------------------------

回答by Pawe? Polewicz

You didn't specify how many files are there or what is the desired output. Is this what You are looking for:

您没有指定有多少文件或所需的输出是什么。这是你想要的:

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

回答by Motiejus Jak?tys

Yet another variation :)

另一个变化:)

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

Edit: this will give the total sum, instead of file-by-file.

编辑:这将给出总和,而不是逐个文件。

Edit2: Add .after findto make it work

Edit2:添加.之后find使其工作

回答by jonhattan

Surprisingly there's no answer based on find's -execand awk. Here we go:

令人惊讶的是,没有基于 find-exec和 的答案awk。开始了:

find . -type f -exec wc -l {} \; | awk '{ SUM += 
find . -name '*.py' -exec wc -l '{}' \; | awk '{ SUM += 
wc $(find . -type f | egrep "\.(h|c|cpp|php|cc)" )
; } END { print SUM; }'
} END { print SUM }'

This snippet finds for all files (-type f). To find by file extension, use -name:

此代码段查找所有文件 ( -type f)。要按文件扩展名查找,请使用-name

find . -name '*.php' -type f -exec wc -l {} \;
# faster, but includes total at end if there are multiple files
find . -name '*.php' -type f -exec wc -l {} +

回答by sergeych

More common and simple as for me, suppose you need to count files of different name extensions (say, also natives)

对我来说更常见和简单,假设您需要计算不同名称扩展名的文件(例如,也是本地人)

find . -name '*.php' -type f | sort | xargs -L1 wc -l
# for files with spaces or newlines, use the non-standard sort -z
find . -name '*.php' -type f -print0 | sort -z | xargs -0 -L1 wc -l

Thank you for the feedback, I've corrected it.

谢谢你的反馈,我已经更正了。

回答by Paul Draper

POSIX

POSIX

Unlike most other answers here, these work on any POSIX system, for any number of files, and with any file names (except where noted).

与此处的大多数其他答案不同,这些适用于任何 POSIX 系统、任何数量的文件和任何文件名(除非另有说明)。



Lines in each file:

每个文件中的行:

find . -name '*.php' -type f -exec wc -l {} \; | sort -nr
# faster, but includes total at end if there are multiple files
find . -name '*.php' -type f -exec wc -l {} + | sort -nr

Lines in each file, sorted by file path

每个文件中的行,按文件路径排序

find . -name '*.php' -type f -exec cat {} + | wc -l

Lines in each file, sorted by number of lines, descending

每个文件中的行数,按行数降序排列

##代码##

Total lines in all files

所有文件中的总行数

##代码##