Search and Replace a string in many files recursively on unix

find /net/hc287/summetj/research/dist1 -name "Root"
-exec perl -pi.bak -e's/summetj\@flux.cc.gatech.edu:/summetj\@wilks1.cc.gatech.edu:/g' {} \;

The above command line will find all files named "Root" recursively under the  /net/hc287/summetj/research/dist1 direcotry and execute perl to replace the string "summetj\@flux.cc.gatech.edu:" with the string "summetj\@wilks1.cc.gatech.edu:"  It also keeps a backup of all the changed files as Root.bak, just in case….

The {} is replaced by the find command with the name of the file, while the \; tells find's -exec command where the program arguments end. The @ signs are escaped by \'s.  This is useful if you need to search and replace a string in many files in a directory tree to update some settings, for example, if the CVSROOT machine has been changed from flux to wilks1 and you don't want to do a full new checkout of 45MB over a slow link.

Leave a Reply

Your email address will not be published. Required fields are marked *