Skip to content

Instantly share code, notes, and snippets.

@kckrinke
Last active November 9, 2016 21:07
Show Gist options
  • Save kckrinke/be475877c06e9429d65e26f3b259d9df to your computer and use it in GitHub Desktop.
Save kckrinke/be475877c06e9429d65e26f3b259d9df to your computer and use it in GitHub Desktop.
Conveniently find source code files (based on file extensions).
#!/bin/bash
usage() {
while [ $# -gt 0 ]; do echo "$1"; shift; done
echo "usage: $(basename $0) [options] [path ...]"
echo "options:"
echo " -h This usage information screen"
echo " -n Dry run, just print command"
echo " -t Explicit source file extn"
echo
echo "Note: defaults to \"find-src -t sh -t pl -t php -t py\""
exit 1
}
DRY_RUN=0
CONDS=()
while getopts ":?hnt:" ARG "$@"
do
case "${ARG}" in
"?"|"h")
usage
;;
"n")
DRY_RUN=1
;;
"t")
[ ${#CONDS[@]} -gt 0 ] && CONDS+=("-o")
CONDS+=("-name" "\"*.${OPTARG}\"")
;;
"--")
break
;;
esac
done
shift $((OPTIND-1))
if [ ${#CONDS[@]} -eq 0 ]
then
CONDS+=("-name" "\"*.sh\"")
CONDS+=("-o")
CONDS+=("-name" "\"*.pl\"")
CONDS+=("-o")
CONDS+=("-name" "\"*.php\"")
CONDS+=("-o")
CONDS+=("-name" "\"*.py\"")
fi
COMMAND=()
COMMAND+=("find")
COMMAND+=("$@")
for c in "${CONDS[@]}"
do
COMMAND+=("${c}")
done
if [ $DRY_RUN -eq 1 ]
then
echo "${COMMAND[@]}"
exit 0
fi
eval ${COMMAND[@]}
exit $?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment