Bash find string

Since native recursive grep only works sometimes (not acceptable), and typing ‘find . -name *.ext | xargs grep searchString’ was tedious and not nearly saxy enough. This does recursive searching and even highlights the matches!

I use this little diddy for text searching. I found it here.

function fstr()
{
    OPTIND=1
    local case=""
    local usage="fstr: find string in files.
Usage: fstr [-i] \"pattern\" [\"filename pattern\"] "
    while getopts :it opt
    do
        case "$opt" in
        i) case="-i " ;;
        *) echo "$usage"; return;;
        esac
    done
    shift $(( $OPTIND - 1 ))
    if [ "$#" -lt 1 ]; then
        echo "$usage"
        return;
    fi
    local SMSO=$(tput smso)
    local RMSO=$(tput rmso)
    find . -type f -name "${2:-*}" -print0 | \
xargs -0 grep -sn ${case} "$1" 2>&- | \
sed "s/$1/${SMSO}\0${RMSO}/gI" | \
less
}
  1. #1 by Rich Freedman on May 11, 2008 - 7:46 pm

    Nice. I’m stealing it!
    And ff() too!

  2. #2 by KattyBlackyard on June 14, 2009 - 11:59 pm

    The article is usefull for me. I’ll be coming back to your blog.

  3. #3 by KonstantinMiller on July 6, 2009 - 11:35 pm

    Hello. I think the article is really interesting. I am even interested in reading more. How soon will you update your blog?

(will not be published)