Skip to content

Instantly share code, notes, and snippets.

@tangoabcdelta
Created July 20, 2026 13:36
Show Gist options
  • Select an option

  • Save tangoabcdelta/55ecd92b386e42948012d7c7344a3e31 to your computer and use it in GitHub Desktop.

Select an option

Save tangoabcdelta/55ecd92b386e42948012d7c7344a3e31 to your computer and use it in GitHub Desktop.
search through all files in a specified directory (recursively) for occurrences of a given string (FOOBAR).

Search all files recursively for a given string

What is it about

  • Shell script that searches through all files in a specified directory (recursively) for occurrences of a given string e.g. FOOBAR.
  • This script can be customized by changing the SEARCH_STRING variable.
  • The script uses grep to find the string and ignores binary files using file -I.

How to Use This Script:

  1. Save the script in a file, for example, search_string.sh.
  2. Make it executable with the command chmod +x search_string.sh.
  3. Run the script from the terminal by providing the directory you want to search in as an argument. For example:
    ./search_string.sh /path/to/directory
  4. If no path is provided, it defaults to the current directory (.).

Explanation of Script:

  • SEARCH_STRING holds the string you want to search for in all files under a specific directory. You can change this to any other term or use command line argument to pass it dynamically.
  • SEARCH_DIR=${1:-"."} allows you to specify the directory to be searched without hardcoding it into the script, using the first argument provided to the script ($1). If no argument is given, it defaults to the current directory (.).
  • grep -rI flag means:
    • -r: Recursively search through directories.
    • -I: Ignore binary files because grep would normally find them and you get confusing output.
    • --exclude='*.o', etc.: These are patterns to exclude specific file types, which are common compiled object files that might clutter the results (depending on your project). You can adjust or expand these excludes as needed for your use case.
  • The script uses file -I implicitly when running grep to filter out binary files, though this is not explicitly mentioned in the script above. If you have older versions of grep that don't support -I, you might need to adjust or replace it with a different approach to exclude binaries based on file content detection (which would be less efficient and more complex).

This script should work well for searching through text files where your string might appear, such as source code in programming languages. Adjust the search patterns (--exclude flags) if you need to broaden or narrow the scope of what is included or excluded from the search.

#!/bin/bash
# Configuration part: change this line to your search term
SEARCH_STRING="FOOBAR"
# Directory to search in, default is current directory
SEARCH_DIR=${1:-"."}
echo "Searching for '$SEARCH_STRING' in $SEARCH_DIR..."
grep -rI --exclude='*.o' --exclude='*.a' --exclude='*.so' --exclude='*.bin' --exclude='*.tmp' --exclude='*.log' "$SEARCH_STRING" "$SEARCH_DIR"
# search through all files in a specified directory (recursively)
# for occurrences of a given string (`FOOBAR`).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment