Skip to content

Instantly share code, notes, and snippets.

@microraptor
Last active March 7, 2020 15:19
Show Gist options
  • Save microraptor/d24d9b3729b9b4a5fc9ba005c5893d6f to your computer and use it in GitHub Desktop.
Save microraptor/d24d9b3729b9b4a5fc9ba005c5893d6f to your computer and use it in GitHub Desktop.
find-deep - Find files or directories with given strings which are in the name or content
#!/bin/bash
####
# Find files or directories with given strings which are in the name or content
#
# Usage: find-deep PATH QUERY-STRING...
# Multiple strings can be passed.
# Ignores case and colors the output.
# Uses these two commands for each string:
# find PATH -iname *QUERY-STRING*
# grep -rni --include='*' --exclude='' -F QUERY-STRING PATH
####
readonly YELLOW=$(echo -en '\e[00;33m')
readonly CYAN=$(echo -en '\e[00;36m')
readonly RESET=$(echo -en '\e[0m')
if [ -n "$1" ] && [ -d "$1" ]; then
SEARCH_PATH="$1"
echo Searching in directory: "$SEARCH_PATH"
TIME_START=$(date +%s)
for query in "${@:2}"; do
echo "${YELLOW}Searching for file or directory names containing:${RESET} ${CYAN}${query}${RESET}"
find "$SEARCH_PATH" -iname "*${query}*" | grep --color --ignore-case --extended-regexp "${query}|$"
echo
echo "${YELLOW}Searching for files containing:${RESET} ${CYAN}${query}${RESET}"
grep --color --recursive --line-number --ignore-case --fixed-strings "$query" "$SEARCH_PATH"
echo
done
TIME_END=$(date +%s)
echo Search finished in $((TIME_END-TIME_START)) seconds
else
echo "Directory not found: $1"
echo "Usage: find-deep PATH QUERY-STRING..."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment