Recursively Get a Line Count in Linux/Unix Using Perl

Today I came to a good stopping point on the web app I have been working on at work and I decided to write up a little perl one liner to see how many lines of code I had written so far. This one liner is recursive so I suppose it could be used to tell you how many lines of anything there is on your system, provided that 'find' could output the file contents (I have a sneaking suspicion that binary files won't work too hot for this). From within the top directory of your project (or whatever you want to get a count for), run the following command... find. | xargs wc -l | grep total | perl -ne 'BEGIN{ $counter=0; } $_=~s/total//; $counter+=$_; END{ print $counter; }' That should do it!

Firstly, this command prints the contents of every file recursively (*). We take that output and pipe it into xargs wc -l, which performs a line count. However, if you give wc -l enough text, it still start totaling sections. With that, it's far easier to simply grep for the word total, and add those numbers. Once we just have a few lines with the total line counts of each section, we pipe that into Perl where we add each of the numbers up and print the total.

I'm sure there's probably a more efficient way of doing this, but this one was to refresh my memory on Perl (it's been a while unfortunately).

If anyone has any better ways of doing this, please post them in the comments section. Everyone likes learning new stuff.