Please create an account to participate in the Slashdot moderation system


Forgot your password?
DEAL: For $25 - Add A Second Phone Number To Your Smartphone for life! Use promo code SLASHDOT25. Also, Slashdot's Facebook page has a chat bot now. Message it for stories and more. Check out the new SourceForge HTML5 Internet speed test! ×

Journal karniv0re's Journal: Lines of Code Counter 2

Here's a quick little script that counts all the lines of code in my ColdFusion projects.

It can easily be adjusted to count other files.


# Count lines of code starting at the parent directory

xmlfiles=`find . | grep "\.xml$"`;
cfmfiles=`find . | grep "\.cfm$"`;
cfcfiles=`find . | grep "\.cfc$"`;
jsfils=`find . | grep "\.js$"`;


for file in $xmlfiles
total="$total + `wc -l $file | awk {'print $1'}`";

for file in $cfmfiles
total="$total + `wc -l $file | awk {'print $1'}`";

for file in $cfcfiles
total="$total + `wc -l $file | awk {'print $1'}`";

for file in $jsfiles
total="$total + `wc -l $file | awk {'print $1'}`";

echo $total | bc -q

This discussion has been archived. No new comments can be posted.

Lines of Code Counter

Comments Filter:
  • try total=$((total + `wc -l $file | awk {'print $1'}`))

    Other things:
    instead of grep try
    find . -name '*.xml'
    or even
    find . -name '*.xml' -o -name '*.cfm' -o -name '*.cfc' -o ...

    If you know that there's fewer than ~5000 files (bash's maximum # of arguments on a command line, compile-time option so YMMV) then you can pull it off in one line even:
    total=$(wc -l $(find . -name '*.xml' -o -name '*.cfm' -o -name '*.cfc' -o -name '*.js') | awk '/total$/ {print $1}')

    • Thanks Qzukk! I had originally tried bash arithmetic, but I was getting syntax errors, so I went with what I knew. :-) The find arguments are interesting as well. I imagine it's a little speedier than grepping through all the results. Your comments are appreciated!

The IBM purchase of ROLM gives new meaning to the term "twisted pair". -- Howard Anderson, "Yankee Group"