DEV Community

Cover image for How to count lines of your code from your SHELL.
darker
darker

Posted on • Updated on

How to count lines of your code from your SHELL.

So, this one is quite simple, let say, you want to count the lines of code you have inside a project, from a file or a whole directory, how are you going to do it ?

Demo

HOW

This is done with a simple bash function

loc(){
    echo -ne "Lines of Code : ";
    if [ -n "$1" ]; then
        find . -wholename $1 | xargs wc -l;
    else
        find . -name '*' -type f | xargs cat | wc -l;
    fi
}
Enter fullscreen mode Exit fullscreen mode

Basically, it will find files depending on your input and use wc to count lines of your code.

You can then add this in your ~/.bashrc and source before use it or save it in separate shell file that you can call and use when you want !

Thanks for reading, feel free to like and/or subscribe for more 🐼.

Top comments (8)

Collapse
 
ccoveille profile image
Christophe Colombier • Edited

Hi darker

Few suggestion s:

find . -wholename $1 | xargs wc -l;

wc -l $1
Enter fullscreen mode Exit fullscreen mode

That's it.

Even if this would be better if the file starts with a "-", such as "-whatever"

wc -l -- $1
Enter fullscreen mode Exit fullscreen mode

You can test with such a file

echo "whatever\nok" > "-strangefile"
Enter fullscreen mode Exit fullscreen mode

Also,

find . -name '*' -type f | xargs cat | wc -l;

I assume you wanted to exclude all hidden folders.

If not, then

find -type f | xargs cat | wc -l;
Enter fullscreen mode Exit fullscreen mode

would have been enough.

But here is what I would write, assuming you want to exclude all hidden files from current folder.

find * -type f | xargs cat | wc -l;
Enter fullscreen mode Exit fullscreen mode

But here is what I would write

find * -type f -print0 | xargs -r -0 cat | wc -l
Enter fullscreen mode Exit fullscreen mode

-print0 to be able to cope with files with spaces in it, then -0 to accept them as xargs parameters

Just create a file with space to see the problem with your script

echo "whatever\nok" > "file with spaces"
Enter fullscreen mode Exit fullscreen mode

Then the -r is about to tell xargs to do not lamentably fail if nothing is found.

Such as creating a folder, go in that folder with cd, then launch your script. It may work, I'm on my phone for days, no computer close to me. But xargs without -r is anti pattern for me.

Same as find | xargs without the -print0 and -0

Collapse
 
ccoveille profile image
Christophe Colombier

I'm wrong with

wc -l $1
Enter fullscreen mode Exit fullscreen mode

Your code was ok with folder while mine doesn't.

So here is what I would suggest:

find "$@" -type f -print0 | xargs -r -0 cat | wc -l
Enter fullscreen mode Exit fullscreen mode

No if branching needed.

$@ allows you to pass any parameters

So you can pass a folder and two files to your script for example

Collapse
 
sanixdarker profile image
darker

NIIIICE !
Thanks for the feedback, i will update that !

Thread Thread
 
ccoveille profile image
Christophe Colombier

Test first, I'm not in front of a computer 😅

Thread Thread
 
sanixdarker profile image
darker

Haha !
Don't worry, i just tested it and it's working !

Thread Thread
 
ccoveille profile image
Christophe Colombier

21 years of Linux experience after all 😅

Thread Thread
 
sanixdarker profile image
darker

yeah, you rock man !

Collapse
 
ccoveille profile image
Christophe Colombier

Your implementation is good proof of concept, but there are commonly accepted implementations

github.com/AlDanial/cloc

dwheeler.com/sloccount/