Blog Master G

Word. And photos, too.

Blog Master G random header image

Unix/Linux Find & Replace in Multiple Files

Thursday, February 20th, 2003 · 19 Comments

Since it’s damn near impossible to find online the simplest way to scan a Unix directory of files, search for one text pattern, and replace with another, I am now archiving the simplest method I could find (which I’ve tested and have proven that it works beautifully). Simply cd to the directory where your files live, modify (or leave) the *.php to match the file type you are modifying, then run the following at the command line:

    for fl in *.php; do
    mv $fl $fl.old
    sed ‘s/FINDSTRING/REPLACESTRING/g’ $fl.old > $fl
    #rm -f $fl.old
    done

Uncomment rm -f $fl.old if you don’t want to bother keeping a copy of the old files. Simple, eh? It’s all about sed, baby.

Linux: Replace a string in several text files

Tags: technology

19 responses so far ↓

  • 1 Indrid Cold // Apr 25, 2003 at 10:23 am

    Awesome little script; thanks for sharing.

  • 2 Matt Toledo // Jun 10, 2003 at 3:26 pm

    Thanks a lot for this. I’ve been looking for a way to do this for quite a while. I’m suprised its not a function of sed or a built in linux command.

    Now if only you can make this recursive through subdirectories!

  • 3 Tobias // Jul 17, 2003 at 8:07 am

    The following script includes subdirectories and capsulates the functionality in a file.

    first arg: File Pattern; e.g ‘*.php’
    second arg: pattern to search for; e.g. “find string”
    third arg: replace string; e.g. “replace string”

    The script still fails on filenames, that contain spaces. Any ideas what’s wrong?

    =============================================
    Paste this into a file ‘renall’ and make it executable (chmod u+x renall):

    #!/bin/sh

    if [ $# -lt 3 ] ; then
    echo -e “Wrong number of parameters.”
    echo -e “Usage:”
    echo -e ” renall ‘filepat’ findstring replacestring\n”
    exit 1
    fi

    #echo $1 $2 $3
    for i in `find . -name “$1” -exec grep -l “$2” {} \;`
    do
    mv “$i” “$i.sedsave”
    sed “s/$2/$3/g” “$i.sedsave” > “$i”
    echo $i
    #rm “$i.sedsave”
    done

  • 4 sean // Jul 18, 2003 at 9:57 am

    Sorry, I’m new to Linux.

    I just can’t seem to get it to work. I copy it to the directory, chmod it, and.. well, I guess my question is if I’m running it properly. Should there be any output?

  • 5 sean // Jul 18, 2003 at 10:11 am

    Ah, nevermind. I got it to work. Thanks.

  • 6 Sleeper // Jul 29, 2003 at 2:46 pm

    Thank you. I had previously found a solution that uses a similar syntax in Perl (in the Linux Cookbook), but it fails to escape characters properly. Your script works perfectly.

  • 7 Harish Puranik // Sep 3, 2003 at 9:46 am

    Thanks for sharing this … was very useful.

  • 8 Gergo // Oct 26, 2003 at 1:58 am

    There is a nice little utility called rpl for this task. http://software.freshmeat.net/projects/rpl/

  • 9 Michael Sewell // Nov 11, 2003 at 10:52 am

    That rpl utility was very useful…it did exactly what I needed. Thanks!

  • 10 Mario // Nov 24, 2003 at 8:24 pm

    Useful little script, thanks πŸ™‚

  • 11 Aleda Freeman // Dec 3, 2003 at 7:31 am

    Thank you, thank you, thank you!
    Just what I needed.

  • 12 Grant // Jan 16, 2004 at 4:05 pm

    This is great! Thanks for the tip!

  • 13 Nick Gushlow // Feb 11, 2004 at 10:37 am

    Nice little script, thanks. πŸ™‚

  • 14 Mandy // Apr 21, 2004 at 9:48 am

    Wonderful! Thanks!

  • 15 Sway // Apr 21, 2004 at 5:35 pm

    Just what I was looking for! THX!

  • 16 Megan // May 4, 2004 at 5:59 pm

    Wow! How easy. Thanks!!

  • 17 scott // May 22, 2004 at 8:44 pm

    The following works for me. The command on the web page you reference has an error (see my comment there). It should be:

    find ./ -type f -exec sed ‘s/string1/string2/’ {} \;

    This command visits every file in . and it’s subdirectories and makes the substitution.

  • 18 scott // May 22, 2004 at 9:57 pm

    oops, forgot the -i option to make sed edit the file in-place:
    find ./ -type f -exec sed -i ‘s/string1/string2/’ {} \;

  • 19 KristyX // Nov 4, 2004 at 2:05 am

    Thanks a bunch! This is exactly what I needed πŸ™‚ Will save me tons of repetitive, mindless work heh