Created
November 14, 2019 17:17
-
-
Save brimworks/251290b1566926e6929025d0757a1312 to your computer and use it in GitHub Desktop.
grep-r
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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[@]}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.