Linux / Unix Tips and Tricks

This blog contains Linux / Unix Tips and Tricks, collected during my work as a Unix System Administrator.
They are here for my own reference, but they might help you as well !

Replacing text in multiple files

len | 15 December, 2008 07:42

Here's a perl oneliner that does text search & replace over multiple files. Make sure you create a backup before using it !

This example will replace all occurences of "oldserver" to "newserver" in the *.conf files:

$ perl -pi -e 's/oldserver/newserver/g' *.conf

If you combine it with find you can even do search and replace recursively over directories.
This example will replace all occurences "10.0.0.1" to "192.168.1.1" in all files under /data:

$ find /data -type f | xargs perl -pi -e 's/10.0.0.1/192.168.1.1/g'

 

 
Accessible and Valid XHTML 1.0 Strict and CSS
Powered by LifeType - Design by BalearWeb