Tuesday, March 3, 2009

Find and Replace several files

If you need to change the same thing in hundreds of files then following script is for you. It will create the backup of every file with .bak extension.

perl -pi.bak -e 's/your-find-string/your-replace-string/g' filename

Example:
perl -pi.bak -e 's/abc/xyz/g' *.php 

This will replace abc with xyz in all the files in current directory which are with .php extension.

1 comment:

  1. This will work effectively for 'find and replace' in folders/sub-folders:

    If all the files are with .xml extension:

    find . -name "*.xml" | while read a; do perl -pi.bak -e 's/source/target/g' $a; done

    ReplyDelete