Recursive grep (grepr) for Solaris

So if you’re a Linux user that’s ever used Solaris, you know that a lot of the
commands don’t translate exactly the same. One such issue is the lack of a
recursive flag (-R) on the grep command. Not a problem though, bash is a
powerful thing. Here’s my solution to the problem:

#!/bin/bash

# Simulates the Linux grep with the recursive flag -R Originally built to
# keep my sanity on older Sun Solaris installs.
#
# @author Josh Sherman
# @link http://joshtronic.com

if [ -z "$1" ] then
    echo "Usage: grepr.sh pattern path"
    exit
fi

if [ -z "$2" ] then
    SEARCH_PATH="."
else
    SEARCH_PATH="$2"
fi

for i in `find $SEARCH_PATH` do
    RESULTS=$(grep "$1" $i)

    if [ "$RESULTS" ] then
        echo
        echo "$i:"
        echo "$RESULTS"
    fi
done
Josh Sherman - The Man, The Myth, The Avatar

About Josh

Husband. Father. Pug dad. Musician. Founder of Holiday API, Head of Engineering and Emoji Specialist at Mailshake, and author of the best damn Lorem Ipsum Library for PHP.


If you found this article helpful, please consider buying me a coffee.