Skip to content

Instantly share code, notes, and snippets.

@brimworks
Created November 14, 2019 17:17
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 brimworks/251290b1566926e6929025d0757a1312 to your computer and use it in GitHub Desktop.
Save brimworks/251290b1566926e6929025d0757a1312 to your computer and use it in GitHub Desktop.
grep-r
#!/bin/bash -e
#
# Usage: grep-r PATTERN [ GREP-ARGS ] [ -- [ DIRECTORIES ] [ FIND-ARGS ] ]
#
# PATTERN - The pattern to grep within files for
# GREP-ARGS - A list of arguments to pass to grep, for example -E for extended regex
# DIRECTORIES - A list of directories to seach (may not begin with `-`)
# FIND-ARGS - A list of arguments to pass to find, for example `-regex '.*/[fF]ile.txt'`
#
# Defaults:
# GREP-ARGS: none
# DIRECTOREIS: .
# FIND-ARGS: -true
declare -a grepArgs
while [ $# -gt 0 ]; do
case "$1" in
--)
shift
break
;;
esac
grepArgs[${#grepArgs[*]}]="$1"
shift
done
declare -a directories
while [ $# -gt 0 ]; do
case "$1" in
-*) break ;;
*)
directories[${#directories[*]}]="$1"
shift
;;
esac
done
if [ ${#directories[*]} -eq 0 ]; then
directories[0]="."
fi
if [ $# -eq 0 ]; then
set -- -true
fi
find -E "${directories[@]}" \! \( \( -name dump.sql -o -name data.json -o \
\( -type d \( -name logs -o -name auditLogs -o -name docker_container -o -name node_modules -o -name .git -o -regex '.*/target/(test-)?classes' \) \) \
\) -prune \) -type f \( "$@" \) -print0 | xargs -0 grep "${grepArgs[@]}"
@brimworks
Copy link
Author

I update this script with more directories and files to exclude as I see fit... if you use this script, you will probably also want to customize what is skipped by default.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment