Skip to content

Instantly share code, notes, and snippets.

@jordwalke
Created November 8, 2017 01:56
Show Gist options
  • Save jordwalke/823aaf20ab28d4d85306eb5721e9ac9a to your computer and use it in GitHub Desktop.
Save jordwalke/823aaf20ab28d4d85306eb5721e9ac9a to your computer and use it in GitHub Desktop.
Better find.
# https://stackoverflow.com/a/28938235
fnd () {
UNDER_ON=`tput smul`
UNDER_OFF=`tput rmul`
BOLD=`tput bold` # Select bold mode
BLACK=`tput setaf 0`
RED=`tput setaf 1`
GREEN=`tput setaf 2`
YELLOW=`tput setaf 3`
BLUE=`tput setaf 4`
MAGENTA=`tput setaf 5`
CYAN=`tput setaf 6`
WHITE=`tput setaf 7`
RESET=`tput sgr0`
if [ -z "$1" ]; then
printf "\n"
printf "${BOLD}${GREEN}fnd: Finds files and directories avoiding common garbage directories.${RESET}\n"
printf "\n"
printf " ${BOLD}fnd root${RESET}\n"
printf " Finds all files from a root\n"
printf "\n"
printf " fnd .\n"
printf " fnd ./relativePath\n"
printf "\n"
printf " ${BOLD}fnd root namePattern${RESET}\n"
printf " Finds files from root, matching a -name pattern. Place name pattern in single quotes.\n"
printf "\n"
printf " fnd path '*.re'\n"
printf " fnd path '*.txt'\n"
printf " fnd path 'filename.*'\n"
printf " fnd path 'Basena' # Does not match files named Basename\n"
printf " fnd path 'Basena*' # Matches file named Basename\n"
printf "\n"
printf " ${BOLD}fnd root namePattern command with args${RESET}\n"
printf " Finds from root, matching name pattern, but also executes a command with arguments.\n"
printf " The '{}' will be substituted with the file name found.\n"
printf "\n"
printf " fnd path '*.re' echo '{}'\n"
printf " fnd path '*.re' ls -al '{}'\n"
printf "\n"
printf "\n"
else
if [ -z "$2" ]; then
find "$1" -path \*/.git -prune -o -path \*/.hg -prune -o -path \*/node_modules -prune -o -path \*/_build -prune -o -print
else
if [ -z "$3" ]; then
# Second, but no third arg
find "$1" -path \*/.git -prune -o -path \*/.hg -prune -o -path \*/node_modules -prune -o -path \*/_build -prune -o -name "$2" -print
else
# Second and third arg. Shift them away so that $@ is the remaining command
DIR_ROOT="$1"
NAME_PAT="$2"
shift
shift
find "$DIR_ROOT" -path \*/.git -prune -o -path \*/.hg -prune -o -path \*/node_modules -prune -o -path \*/_build -prune -o -name "$NAME_PAT" -exec "$@" \;
fi
fi
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment