This script will recursively search files in a directory for a pre-defined string.
Usage: $ bash search-string.sh "KEYWORD" /path/to/directory
Get file:
$ wget https://bytesofprogress.net/resources/bash-tools/search-string/search-string.sh
Contents of file:
#!/bin/bash
# This script will recursively search files in a directory for a pre-defined string.
# Usage: $ bash search-string.sh "KEYWORD" /path/to/directory
# Did the prompt have 2 arguments?
if [ "$#" -ne 2 ]; then
echo "Usage: $0 QUERY PATH_TO_FOLDER"
exit 1
fi
QUERY="$1"
SEARCHDIR="$2"
# Check if given path exists and is a directory.
if [ ! -d "$SEARCHDIR" ]; then
echo "Error: '$SEARCHDIR' is not a directory."
exit 1
fi
# Searching recursively, echo paths.
grep -rl --binary-files=without-match "$QUERY" "$SEARCHDIR"