Skip to content

Instantly share code, notes, and snippets.

@astorije
Created March 19, 2016 20:17
Show Gist options
  • Save astorije/e7d12ff484cbfc745ec1 to your computer and use it in GitHub Desktop.
Save astorije/e7d12ff484cbfc745ec1 to your computer and use it in GitHub Desktop.
Counts the number of occurrences of a given pattern recursively in the current directory.
#!/bin/sh
set -e # Stop execution at first error
if [[ $# -eq 0 ]] ; then
echo "Usage: $(basename $0) pattern"
echo " Counts the number of occurrences of a given pattern recursively"
echo " in the current directory."
exit 1
fi
grep "${1}" -I --recursive . | # -I Ignore binary files
cut -d ':' -f1 | # -d Field delimiter
# -f1 First field
uniq |
wc -l | # -l Number of lines
awk '{print $1}' # Only print the integer without whitespaces
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment