Thursday, April 16, 2009

One line find and replace string command in Linux/Unix.

Hi,

One of my friend was in urgent need for a find and replace command in set of files of some specific extension and asked me if i have one.

I gave him the following: Hope this is useful for you.

find . -name "*.extension" -exec sed -i s/strtofind/strtoreplace/g '{}' \;

where,
extension- extension of the file names where you want to find and replace.
strtofind- string to be found in those files.
strtoreplace- string to be replaced in those files.

Note: Please take caution when you want to find and replace strings having "/". You may need to use \\/ to make sure that "/" is treated as string by the sed command.

No comments: