Wazery
Wazery

Reputation: 15863

How to calculate the number of lines in source code

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

Upvotes: 10

Views: 14167

Answers (7)

ismail
ismail

Reputation: 47572

Use sloccount

 "SLOCCount", a set of tools for counting physical Source Lines of Code (SLOC) in a large number of languages of a potentially large set of programs

Upvotes: 23

Fritz G. Mehner
Fritz G. Mehner

Reputation: 17188

Use cloc. It supports about 80 languages.

Upvotes: 11

Fatih Acet
Fatih Acet

Reputation: 29529

You can just use

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

Upvotes: 12

Kevin
Kevin

Reputation: 1921

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

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.

Upvotes: -1

Michael Lorton
Michael Lorton

Reputation: 44376

My suggestions would be

  1. Use a find command as in Barti's answer to locate all the files
  2. Use sed or something to strip out all the comments
  3. Don't do it at all

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.

Upvotes: 1

Dr G
Dr G

Reputation: 4017

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

find . | wc

Upvotes: -1

barti_ddu
barti_ddu

Reputation: 10299

You could try something like:

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

Upvotes: 3

Related Questions