Skip to content

Instantly share code, notes, and snippets.

@dkomando
Last active November 7, 2017 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dkomando/6614254813accea58831d79e48a68169 to your computer and use it in GitHub Desktop.
Save dkomando/6614254813accea58831d79e48a68169 to your computer and use it in GitHub Desktop.
Unix CLI - Line Count Function
# -----------------------------------------------
# Specific file type line count function.
# - e.g. great tool for js|css|sass|less|html
# - Tested & working on MacOSX 10.12.*
# - Requirements: Unix based OS & Git.
# Git is needed since I needed a count that
# obeys the .gitignore file.
# -----------------------------------------------
# Place this function in your ~/.bashrc file:
# Usage:
# After adding snippet run:
# $ exec bash
# NOTE: Each time you reopen a new MacOS Terminal
# you will need to type: exec bash
# From there you can navigate into your project
# folder run this snippet with e.g.:
# $ linecount html
# To count all the *.HTML files in your current
# directory and it's child directories
function linecount() {
echo "Checking all $1 documents";
FILETYPE='.*\.'$1'$';
TOTALFILES=$(git ls-files | grep $FILETYPE | wc -l | awk {'print $1'})
echo ">> Total Files: "$TOTALFILES;
# If no files found, don't continue and exit!
if [ $(($TOTALFILES)) -gt 0 ]
then
grep -c '^' $(git ls-files | grep $FILETYPE) | awk -F ":" {'print $2 " - " $1'}
TOTALLINES=$(wc -l $(git ls-files | grep $FILETYPE) | grep total | awk {'print $1'})
echo ">> True Line Count Total: "$(($TOTALLINES+$TOTALFILES));
else
echo "No specified file types found, exiting!";
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment