Skip to content

Instantly share code, notes, and snippets.

@Willshaw
Last active August 29, 2015 14:19
Show Gist options
  • Save Willshaw/db4110e5c52e5e2a37fa to your computer and use it in GitHub Desktop.
Save Willshaw/db4110e5c52e5e2a37fa to your computer and use it in GitHub Desktop.
Bash Script to Git Grep a directory of repos
#!/bin/bash
#
# Supply 2 params from cmd, path to search and the term.
# The script will cd to $path, then check any directories
# it finds for the presence of a .git directory.
#
# If it finds a .git directory, a repo is assumed and
# a case insensitive grep is performed on term
#
# example:
# ./script.sh /var/www/vhosts foobar
#
# will perform a `git grep -i "foobar"`
# for all paths that match /var/www/vhosts/*/.git
#
# get params and put in meaningful vars
path=$1
term=$2
# move to path
cd $path
# loop over all directories - the / limits the query to dir only
for d in */
do
# move into the specific directory
cd $path$d
# if .git is found, echo progress and perform grep
if [ -d ".git" ]
then
echo "checking git repo in \"$d\" for \"$term\""
git grep -i "$term"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment