Tim's IT-Blog

Just a blog about IT and IT-Problems…

Shell: Find and Repleace String over many files with Perl

by admin_import on 03/05/2010

From: http://snippets.dzone.com/posts/show/116

An equivalent of the other find-replace, except it’s a one-liner that generates no temp files, and is more flexible:

perl -pi -e 's/find/replace/g' *.txt

Or, to change matching files in a hierarchy:

find . -name '*.txt' |xargs perl -pi -e 's/find/replace/g'

That didn’t work when the file names contained white space. This seems to work even with them (with GNU find and xargs):

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