Ever wanted to have some basic stats of your projects?
Like:
./stats.sh
########################################
Project stats for "tui"
########################################
260 kb in bin
24 kb in conf.etc
12 kb in conf.home
32 kb in docs/samples
176 kb in docs/wiki
280 kb in docs
192 kb in man
1984 kb in screenshots
12 kb in templates/manpage
32 kb in templates/scripts
12 kb in templates/usr
64 kb in templates
36 kb in themes
13 folders with a total of 3116 kbytes
########################################
Spread across 190 files, there are:
Lines Total: 14542
Comment lines: 2409
Blank lines: 201
Avrg lines p. file: 76
Was created using these few lines:
#!/bin/bash
#
# Vars
#
LINER="########################################"
COMMENTS=0
LINES=0
BLANKS=0
#
# Action
#
echo "$LINER"
echo -e "\tProject stats for \"$(basename $PWD)\""
# Size
echo "$LINER"
for DIR in * ; do [ -d "$DIR" ] && LIST+=" $DIR" ; done
du $LIST | awk '{print $1" kb\tin "$2;SUM=SUM+$1} END {print NR" folders with a total of "SUM" kbytes"}'
# Files
echo "$LINER"
FILES=$(find|grep -ve ".git" -ve ".jpg"| wc -l)
echo "Spread across $FILES files, there are:"
# Lines
for F in $(find|grep -ve ".git" -ve ".jpg")
do [ -f "$F" ] && \
COMMENTS=$(( $COMMENTS + $(grep ^"#" "$F" | wc -l) )) && \
LINES=$(( $LINES + $(cat "$F" | wc -l) )) && \
BLANKS=$(( $BLANKS + $(grep ^[[:space:]]$ "$F" | wc -l) ))
done
# Summary
echo -e "Lines Total: \t\t $LINES"
echo -e "Comment lines: \t\t $COMMENTS"
echo -e "Blank lines: \t\t $BLANKS"
echo -e "Avrg lines p. file: \t $(( $LINES / $FILES ))"
I wanted to share it when i wrote it, but figured i didnt.
Its printing (almost) the same stats in quite near range (imo, regarding the task done)....
Org:
real 0m0.581s
user 0m0.195s
sys 0m0.742s
New:
real 0m0.402s
user 0m0.385s
sys 0m0.026s
Only value changed is 'blank lines', raised from ~200 to ~1800.
Eventhough i've (theoreticly) removed more image files - which shouldnt contain empty lines anyway....
I wonder if you want to skip .git directories.
Then a -prune in find is more efficient.
Also it is more efficient to run the find once, and count the FILES in the loop.
FILES=0
for F in $(find . -type d -name ".git" -prune -o -type f \! -name ".jpg" -print)
do
((FILES+=1))
... # only files, dont need to test again
done
NB the -print is needed: by explicitly printing the files it will not print the pruned directories.