bash 如何计算源代码中的行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4535183/
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-17 23:10:02  来源:igfitidea点击:

How to calculate the number of lines in source code

bashterminallines-of-code

提问by Wazery

As the title says how i can calculate the total number of lines in a source code folder using bash commands

正如标题所说,我如何使用 bash 命令计算源代码文件夹中的总行数

回答by ismail

回答by Fatih Acet

You can just use

你可以使用

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

回答by Fritz G. Mehner

Use cloc. It supports about 80 languages.

使用时钟。它支持大约 80 种语言。

回答by barti_ddu

You could try something like:

你可以尝试这样的事情:

find . -name "*.java" -exec cat {} \; | wc -l

回答by Malvolio

My suggestions would be

我的建议是

  1. Use a findcommand as in Barti's answer to locate all the files
  2. Use sedor something to strip out all the comments
  3. Don't do it at all
  1. 使用findBarti's answer 中的命令来定位所有文件
  2. 使用sed或某物去除所有评论
  3. 根本不要这样做

SLOC is a very, very misleading way to measure software. Bill Gates said it was like estimating the quality of an aircraft by weight, and it may be the only helpful thing he ever said.

SLOC 是一种非常非常具有误导性的衡量软件的方法。比尔盖茨说这就像用重量来估计一架飞机的质量,这可能是他说过的唯一有用的话。

回答by Kevin

Already been answered, just giving another way using awkwhich you'll definitely have.

已经回答了,只是提供另一种awk你肯定会使用的方法。

cat *.ext | awk 'BEGIN{i=0;} {i++;} END{print "Lines ", i}'

cat *.ext | awk 'BEGIN{i=0;} {i++;} END{print "Lines ", i}'

I only also suggest this because it can be easily edited to add patterns (such as comments) for lines that you don't want to count.

我也只是建议这样做,因为它可以很容易地编辑为您不想计算的行添加模式(例如注释)。

回答by Dr G

This will count empty lines as well, but it's easy. Go to the specific directory you wanna check and do

这也将计算空行,但这很容易。转到您要检查并执行的特定目录

find . | wc