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 ?
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
}
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)
Hi darker
Few suggestion s:
That's it.
Even if this would be better if the file starts with a "-", such as "-whatever"
You can test with such a file
Also,
I assume you wanted to exclude all hidden folders.
If not, then
would have been enough.
But here is what I would write, assuming you want to exclude all hidden files from current folder.
But here is what I would write
-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
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
I'm wrong with
Your code was ok with folder while mine doesn't.
So here is what I would suggest:
No if branching needed.
$@
allows you to pass any parametersSo you can pass a folder and two files to your script for example
NIIIICE !
Thanks for the feedback, i will update that !
Test first, I'm not in front of a computer 😅
Haha !
Don't worry, i just tested it and it's working !
21 years of Linux experience after all 😅
yeah, you rock man !
Your implementation is good proof of concept, but there are commonly accepted implementations
github.com/AlDanial/cloc
dwheeler.com/sloccount/