Return to Snippet

Revision: 48735
at July 8, 2011 08:29 by JRummy16


Initial Code
countLines() {

	if [ $# -ne 1 ]; then
		echo "Usage: countLines <file extension>"
		return
	fi
	
	N=0
	CURRENT=0
	INCREMENTS=5
	FILES=` find . -type f -name "*.$1" | wc -l `
	FOO=">"
	BAR=""
	
	find . -type f -name "*.$1" -print |
	while read file; do
	
		LINES=` wc -l $file | awk '{print $1}' `
		N=$(( $N + $LINES ))
		CURRENT=$(( $CURRENT + 1 ))		
		PERCENT=$(( $CURRENT * 100 / $FILES))
		
		if [ $(( $PERCENT - $INCREMENTS )) -gt 5 ]; then
			INCREMENTS=$(( $INCREMENTS + 5 ))
			BAR="${BAR}="
		fi
		echo -ne "\r\t[ "
		echo -ne "${BAR}${FOO} ] ${PERCENT}% "
		
		if [ $CURRENT -eq $FILES ]; then
			echo "complete!"
			echo
			echo " File extension.... $1"
			echo " File count ....... $FILES files"
			echo " Line count ....... $N lines"
			echo
		fi
	done

}

Initial URL


Initial Description
The progress bar snippet found on this site was used. This counts all lines and files in working directory for specified file extension. I am no pro at bash, just a beginner. I'm sure it can be done better but still useful.

Initial Title
Count lines of all files with specified file extension

Initial Tags


Initial Language
Bash