I was trying to find a solution to do a find & replace across multiple files which was purely command line based. There are plenty of scripts out there which will accomplish this but I needed a single line command. After some google searches and some experimentation I came up with this snippet.

grep  -Ril matchstring  somedir/ | xargs sed -i ‘s/string1/string2/g’


grep  -Ril ‘’  $ORACLE_HOME/ | xargs  sed -i ‘s/ /mts01.amts.local/g’


It looks a bit complicated but it’s quite simple. There are three components to the command:

grep -rlw  matchstring  somedir –  will look into directory $ORACLE_HOME to match all files contain word ‘’  ,

i stands for upper/lower case (optional in your case).

R stands for recursive.

l stands for “show the file name, not the result itself`. This will give you output like this:




xargs- This command is used when you want to pass a lot of arguments to one command. xargs will combine the single line output of find and run commands with multiple arguments, multiple times if necessary to avoid the max chars per line limit. In this case we combine xargs with sed

sed -i ‘s/foo/bar/g’ – aka Stream Editor is a tool which should be in every sys admin’s toolkit.In this case every occurrence of “” is replaced by “mts01.amts.local” in all the files found using the “grep” command. Sed simply parses input and applies certain text transformations to it.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s