HOWTO replace strings in files with Linux

October 29, 2007 – 5:46 pm

So, you want an easy to to replace text in files, even multiple files, on a linux machine, from the command line.
How? - You ask? Easy.

Let me show you.
Let’s say you want to replace all instances of “John” with “Pete” in a file, called resume.txt
In a bash shell, run the following: “replace John Pete — resume.txt

Now, how about replacing all instance of “/home/” with “/home2/”? in all files located in /etc/? (let’s say you have moved the home folder)
“replace “/home/” “/home2/” — /etc/*

Or, you want to update your DNS servers quickly, after moving your servers to a new domain. The old domain was mydomain.com & the new domain is ourdomain.com
replace mydomain newdomain — /var/named/*.db

Share/Save/Bookmark

  1. 2 Responses to “HOWTO replace strings in files with Linux”

  2. Did you mean “strings”, not “stings”? ;)

    By mangoo on Mar 5, 2008

  3. I did, thank you for pointing that out to me :)

    By Rudi Ahlers on Mar 6, 2008

You must be logged in to post a comment.