Wednesday, July 11, 2012

Counting lines of code for your website using linux command


I used the command  below on ubuntu to count the lines of code (LOC) for an entire website 

Go to your website's root directory and run this command


rm -rf temptotal.txt; find . -regex ".*.\(php\|html\|css\|js\)" | xargs  -I FILE  sh -c "echo FILE; cat FILE | tr -d '\t' | tr -s '\n\n' '\n' | wc -l   " | xargs -I COUNT sh -c "echo COUNT;echo COUNT >> temptotal.txt"   ; echo "Total:" ;  cat temptotal.txt | awk '{ sum+=$1} END {print sum}';


Command steps 
1-Delete temp file
2-Recursively scans web files
3- Rmoves tabs and extra endlines and calculate LOC
4- Print LOC and name for each file
5- Saves LOC for each web file in a temp file
5- Count all web files LOC from temp file and prints the total



Edit:

Thanks for Anonymous comment below, you can also use sloccount which can be installed from debian/ubuntu repository

usage:
sloccount /path/to/project/

Note:  sloccount counts only php files (no JS, CSS or HTML)

http://www.dwheeler.com/sloccount/


1 comment: