Use Spotlight to speed up grep searches
Mar 23, '10 07:30:00AM • Contributed by: lullabud
The
grep -ri command (recursively search for matches, ignoring case) in Terminal is incredibly useful, but incredibly slow. In OS X, we can leverage
mdfind to quickly find the files containing our search string, then use
grep to find the strings within only those files. Here's a script to do just that.
#!/bin/bash
#
# Spotlight metadata find and grep by Daniel.Hoherd at gmail dot com
## Check for at least two arguments, print usage if else
if [ $# -lt 1 ] ; then
echo "usage: $0 searchstring [dir or file] [dir2 or file2]"
exit ;
fi
ss=$1;
shift;
files=$@;
until [ -z "$1" ] ; do
thisitem=$1
onlyin="-onlyin '$thisitem' $onlyin"
shift
done;
eval mdfind -0 $onlyin "$ss" | xargs -0 grep -Hi "$ss"
Leveraging
mdfind to eliminate files that do not contain the matching string makes this operation an order of magnitude quicker, cutting it to mere seconds instead of minutes on over 100GB of data in my tests. The downside to this is that Spotlight does not index every directory, so you may be missing some files such as system files.
If you don't want to use this script, the same thing can be accomplished by hand by writing
mdfind -0 -onlyin searchdir searchstring | xargs -0 grep -Hi searchstring. I've included the
-H and
-i options to more closely match the output of
mdfind, but you can safely remove them.