Bash: Search and replace across multiple files

I sometimes need to do a search and replace across lots of files. So, I made a function in my .bashrc to make it easy:

function find_replace {
    FIND=$1
    REPLACE=$2
    echo "Finding $FIND and replacing with $REPLACE"
    grep -rl $FIND .
    grep -rl "$FIND" . | xargs sed -i -e s/$FIND/$REPLACE/
}

Usage: find_replace <search> <replace>
e.g. find_replace hello goodbye

Discussion

blog comments powered by Disqus

Colin Howe

I'm Colin. I like coding, ultimate frisbee and startups. I am VP of engineering at Conversocial