Skip to content

Instantly share code, notes, and snippets.

@dtrip
Created March 24, 2015 08:45
Show Gist options
  • Select an option

  • Save dtrip/d297892df4b40c6f4f6c to your computer and use it in GitHub Desktop.

Select an option

Save dtrip/d297892df4b40c6f4f6c to your computer and use it in GitHub Desktop.

Revisions

  1. dtrip created this gist Mar 24, 2015.
    35 changes: 35 additions & 0 deletions find_and_replace_in_files.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,35 @@
    # *****************************************************************************************
    # find_and_replace_in_files.sh
    # This script does a recursive, case sensitive directory search and replace of files
    # To make a case insensitive search replace, use the -i switch in the grep call
    # uses a startdirectory parameter so that you can run it outside of specified directory - else this script will modify itself!
    #
    # Thanks to: Eliah Kagan <http://askubuntu.com/users/22949/eliah-kagan>
    #
    # *****************************************************************************************

    !/bin/bash
    # **************** Change Variables Here ************
    startdirectory="/your/start/directory"
    searchterm="test"
    replaceterm="test=ok!"
    # **********************************************************

    echo "***************************************************"
    echo "* Search and Replace in Files Version 01-Aug-2012 *"
    echo "***************************************************"

    i=0;

    for file in $(grep -l -R $searchterm $startdirectory)
    do
    cp $file $file.bak
    sed -e "s/$searchterm/$replaceterm/ig" $file > tempfile.tmp
    mv tempfile.tmp $file

    let i++;

    echo "Modified: " $file
    done

    echo " *** All Done! *** Modified files:" $i