Skip to content

Instantly share code, notes, and snippets.

@r10r
Last active December 25, 2015 08:49
Show Gist options
  • Save r10r/6949740 to your computer and use it in GitHub Desktop.
Save r10r/6949740 to your computer and use it in GitHub Desktop.
Combine grep and find to restrict search to certain files.
#!/bin/sh
# Combine find and grep to search (source) files.
# Just for lazy typers ;)
#
# E.g limit grep to *.h and *.c:
# sgrep X509_verify_cert .c .h
find_args=""
for arg in $(echo $@ | cut -d' ' -f 2-); do
if [ -z "$find_args" ]; then
find_args="-name \*${arg}"
else
find_args="$find_args -or -name \*${arg}"
fi
done
command="grep -Rn '$1' \$(find . -type f -and \( $find_args \))"
eval "$command"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment