DEV Community

Cover image for Calculate LoC (Lines of Code) using Bash
Zafer Gurel
Zafer Gurel

Posted on • Edited on

Calculate LoC (Lines of Code) using Bash

Lines of Code (a.k.a. Source Lines of Code) is an important metric in software development.

I've needed a way to count the number of lines in one of the front-end projects I've been working on.

I wanted to give it a try to find a simple solution.

I've written the following shell command to get it done:

find . -type d -regex ".+node_modules" -prune -o -type f -regex ".+\.\(\(tsx?\)\|\(jsx?\)|\(s?css\)\)$" -regextype posix-extended | grep node_modules -v | xargs -I @@ wc -l @@ | awk '{sum+=$ 
1} END {print sum}'

I've tested it on Git Bash and it worked. I guess it will also work on other Linux distros as well. Nothing fancy, just a simple bunch of generic commands piped together.

Let me document it.

-type d -regex ".+node_modules" -prune
Excludes the node_modules folder.
 
-o
Means or and used to combine the other filter.

-type f -regex ".+\.\(\(tsx?\)\|\(jsx?\)|\(s?css\)\)$" -regextype posix-extended
Files with the extensions ts, tsx, js, jsx, css, scss are found. You can add yours to expand the coverage.
 
grep node_modules -v
To the output of find command above, ./node_modules is written even if it's excluded by the prune parameter. Files underneath the pruned folder do not come up but the name of the folder does. So, we find it with grep, use -v parameter to get the excluded lines (inverse selection) and as a result, we clean up the lines containing "node_modules".
 
xargs -I @@ wc -l @@
This takes each line from the previous process, loads it into @@ variable and passes it to wc command. -l means "count the lines". In the output, number of lines and the file names are printed as follows:
1502 a.tsx

awk '{sum+=$1} END {print sum}'
awk, takes the first column (which is the number of lines), iteratively increments the sum variable, then prints the sum.

That's all.

Top comments (0)