/ html / resources / bash-tools / search-string / search-string.sh
search-string.sh
 1  #!/bin/bash
 2  
 3  # This script will recursively search files in a directory for a pre-defined string.
 4  # Usage: $ bash search-string.sh "KEYWORD" /path/to/directory
 5  
 6  # Did the prompt have 2 arguments?
 7  if [ "$#" -ne 2 ]; then
 8    echo "Usage: $0 QUERY PATH_TO_FOLDER"
 9    exit 1
10  fi
11  
12  QUERY="$1"
13  SEARCHDIR="$2"
14  
15  # Check if given path exists and is a directory.
16  if [ ! -d "$SEARCHDIR" ]; then
17    echo "Error: '$SEARCHDIR' is not a directory."
18    exit 1
19  fi
20  
21  # Searching recursively, echo paths.
22  grep -rl --binary-files=without-match "$QUERY" "$SEARCHDIR"