Exclusive WordPress
Just another blog from a WordPressholic.

Search and Replace Text Recursively Linux Command Line

July 26th 2009 in Tips and Tricks by Poer

This one just for my note, a simple linux command line to search and replace text and code within files recursively. Simple, just oneliner, but this oneliner really help me much.

Using combination of Grep and Sed command, the following linux command line will search for ‘http://abc.wordpress.com’ code and replace it with ‘http://abcd.wordpress.com’ (without single quote).

grep -lr -e 'http://abc.wordpress.com/' * | xargs sed -i 's/http:\/\/abc\.wordpress\.com/http:\/\/abcd\.wordpress\.com/g'

Backslashes should be added before all special characters like . and / in the Sed parameters. The general format is like this:

grep -lr -e 'search' * | xargs sed -i 's/search/replace/g'


Leave a Reply



required



required - won't be displayed


Your Comment:

There are a lot of plugin to inject ad code within blog post out there, but most of them is packed with many features that I don’t really need.

If you like me, just need a simple code to inject the ad code inside the blog post in random position, you can use the following code [...]

Previous Entry

Just a slight change in my heart, today I change my blog theme into the beautiful and free Cellar Heat black version.

We can download Cellar Heat from the official site or from Smashing Magazine.

Dark, as dark as my life this last one month hihihihi…

Next Entry