Search & Replace via command line in Linux

I needed to replace domain name in MySQL DB dump which was 700MB, so i’ve googled a bit to find a command line expression. If you need to search/replace in files via command line, you can simply use following command (if perl is installed):

perl -pi -w -e 's/search/replace/g;' *.php
-e means execute the following line of code.
-i means edit in-place
-w write warnings
-p loop

For example we can replace css link in all template files:

As each expression is a regular expression you’ve got to escape the special characters such as forward slash and .

\.\.\/css\/all\.css

So the final line of code ends up as:

perl -pi -w -e 's/\.\.\/css\/style\.css/all\.css/g;' *.php

Leave a Reply

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