Count lines of all files with specified file extension


/ Published in: Bash
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. countLines() {
  2.  
  3. if [ $# -ne 1 ]; then
  4. echo "Usage: countLines <file extension>"
  5. return
  6. fi
  7.  
  8. N=0
  9. CURRENT=0
  10. INCREMENTS=5
  11. FILES=` find . -type f -name "*.$1" | wc -l `
  12. FOO=">"
  13. BAR=""
  14.  
  15. find . -type f -name "*.$1" -print |
  16. while read file; do
  17.  
  18. LINES=` wc -l $file | awk '{print $1}' `
  19. N=$(( $N + $LINES ))
  20. CURRENT=$(( $CURRENT + 1 ))
  21. PERCENT=$(( $CURRENT * 100 / $FILES))
  22.  
  23. if [ $(( $PERCENT - $INCREMENTS )) -gt 5 ]; then
  24. INCREMENTS=$(( $INCREMENTS + 5 ))
  25. BAR="${BAR}="
  26. fi
  27. echo -ne "\r\t[ "
  28. echo -ne "${BAR}${FOO} ] ${PERCENT}% "
  29.  
  30. if [ $CURRENT -eq $FILES ]; then
  31. echo "complete!"
  32. echo
  33. echo " File extension.... $1"
  34. echo " File count ....... $FILES files"
  35. echo " Line count ....... $N lines"
  36. echo
  37. fi
  38. done
  39.  
  40. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.