Two useful perl one-liners for searching and replacing

published Mar 11, 2012 03:15   by admin ( last modified May 04, 2015 05:05 )

 

To change a string in all files in the current directory and below

find |xargs perl -pi -e 's/searchpat/replacepat/g'

 

Examples:

find -name "*.py"|xargs perl -pi -e 's/searchpat/replacepat/g'


find -name "*.pt"|xargs perl -pi -e 's/searchpat/replacepat/g'

 

To change the names of files in current directory and below

find|perl -nl -e '$o= $_;rename($o,$_) if s/searchpat/replacepat/'