Skip to content

Instantly share code, notes, and snippets.

@danthe1st
Last active February 23, 2024 12:23
Show Gist options
  • Save danthe1st/f8e3a7fd93a18b5463aedf8534266911 to your computer and use it in GitHub Desktop.
Save danthe1st/f8e3a7fd93a18b5463aedf8534266911 to your computer and use it in GitHub Desktop.
script for combining find|grep
#!/bin/bash
# combination of find and grep
# Usage:
# findg <text> [<directory>] [<extra args for find>]
set -u
export SEARCH_STRING="$1"
shift
if [ $# -eq 0 ]; then
TARGET_DIR=.
else
TARGET_DIR="$1"
shift
fi
function check {
file="$1"
result="$(grep -i --binary-files=without-match "$SEARCH_STRING" < "$file")"
if [ $? = 0 ]; then
echo "$file"
echo "${result//^/\t}"
fi
}
export -f check
find "$TARGET_DIR" -type f "$@" -exec bash -c 'check "{}"' \; 2>/dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment