Skip to content

Instantly share code, notes, and snippets.

@jranson
Last active January 29, 2021 04:29
Show Gist options
  • Save jranson/7e0db868a8501c908b98dce6e5678670 to your computer and use it in GitHub Desktop.
Save jranson/7e0db868a8501c908b98dce6e5678670 to your computer and use it in GitHub Desktop.
deepgrep (recursive grep with globs)
# add this function to your .bash_profile or .zshrc
#
# usage (all files): deepgrep 'my[Expression]'
# usage (filtered files): deepgrep '*.go' MyString
#
# if your file filter uses a glob wildcard, enclose it in quotes
#
deepgrep() {
if [[ $# -eq 0 || $# -gt 2 ]]
then
print "deepgrep ['*.ext'] expression"
exit
fi
DGFF='*'
if [[ $# -eq 2 ]]
then
DGFF=$1
fi
DGLF=${@: -1}
for file in $(find . -name $DGFF) ; do echo $file; grep -n "$DGLF" $file 2>&1; done | grep -v grep | grep "$DGLF" -B 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment