linux search and replace

It can often be very practical to have a simple search and replace command. I needed this when I wanted to use a template maven project and change that into something different. The “rpl” command is a useful start for this. It can be installed on Ubuntu/Debian using

apt-get install rpl

And then you use it like

rpl -R oldValue newValue .

Which in this case replaces all the (case sensitive) occurrences of the given string in the current directory. There are options to make this case insensitive, check word boundaries etc. Check the man page for more details.

However, this is not enough. You now have files where the references (file and/or directory names) have been modified, but you still need to rename the files themselves. For this the rename command can be used, like (for checking, not doing anything)
rename -n s/oldValue/newValue/ *

This uses a perl expression and renames all files in the current directory. More details can be found here. However, this command is still limited as it does not recursively rename the files. For this you can combine commands and write this as :

rename -n s/oldValue/newValue/ `find . name old-value`

To really do the rename, change the “-n” option to “-v”.

84 Comments

  1. […] Afterwards, I untar in a different directory, replace some magic strings (see here) and voila… […]

Leave a Reply

Your email address will not be published. Required fields are marked *

question razz sad evil exclaim smile redface biggrin surprised eek confused cool lol mad twisted rolleyes wink idea arrow neutral cry mrgreen

*