Search and replace in multiple files in linux

This is a bit off my normal ColdFusion topic, but one worth mentioning. I'm moving an application to a new folder on my linux-based web server. Unfortunately, the app created a series of files in a series of folders that all have the old path. They are generated by the app and only exist on the web server, so I cannot change them locally and re-publish them. In order to change the paths, I used a new-to-me technique called "Perl DASH pie". Now, I haven't actively developed on Perl for years (eg. pre-ColdFusion), but this is a beaut! Here is the syntax that you run from the linux bash prompt:

perl -p -i -e 's/original text/new text/g' [filelist]

How easy is that? For even more power, combine that command with the find command to choose your file list:

perl -p -i -e 's/original text/new text/g' `find . -name '*.cfm'`

(Note the strange mix of single quotes and angled quotes if you are trying to reproduce this!) Very powerful stuff!

Kudos to this Python cookbook entry and this blog post for taking me down this road.

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
I prefer to use this command to replace strings in files:

find -name '*.txt' -print0 | xargs -0 perl -pi -e 's/old/new/g'

because it able to handle files with special symbols.

However, you can look at "rpl" command, which is part of debian/fedora distributions. It has lot of useful options, e.g. --prompt, --keep-times, etc.
# Posted By Volodymyr Lisivka | 12/14/07 11:17 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.