You have no idea how hard it was to refrain from using the command ‘sed’ in a totally ridiculous homophoned title. Have you found yourself wanting to change the domain on an established WordPress installation, or even trying to replicate many times over the same exact WordPress database on a different domain? Yeah, me too. I’ve found while doing plug-in development, it helps to have the same database information copied over when I start with a new branch. Luckily, Linux comes to the rescue with the sed command. It’s a 3 step process. Export your source/current database. Search and replace with sed, and then import the database. Here’s a step by step for you.
Note: you need to have access to a Linux command prompt and the MySQL database via command line in order to perform this by my steps
Export…
First, we need to make an export of your current database, we’ll do this with mysqldump:
$ mysqldump -h yourhostnamehere -u YourUserName -pYourPasswordHere YourDatabaseName > dumpfile.sql
No, I didn’t forget a space after the -p flag. There is no space between the password flag, and the password itself.
Search and Replace
After that completes you should have a file named dumpfile.sql that contains your current database. Now it’s time to search and replace all references to the previous domain (we’ll call it domain-one.com) with our new domain (domain-two.com).
$ sed -i 's/domain-one.com/domain-two.com/g' dumpfile.sql
The ‘-i’ flag is for inline. I simply replaces domain-one.com with domain-two.com where the command finds it. This means, any database settings or posts referencing the domain itself, will be altered, and therefore work when imported….so on to importing.
Importing the newly modified .sql backup
$ mysql -h yourhostnamehere -u YourUserName -pYourPasswordHere YourDatabaseName < dumpfile.sql
This command with the < or less than sign, pushes dumpfile.sql up to the database mentioned.
And there you have it, you just exported, modified, and imported an entire WordPress site including all categories, posts, users, etc…into a new database for use on a different site. Not so bad was it?
For more information on the sed command you can head over to brunolinux.com for a pretty decent sed tutorial and more options.

{ 3 comments… read them below or add one }
pimp
why couldn’t this be done in notepad with a “find and replace” what would be the harm? thanks much for any thoughts
@Austin
You could do this with NotePad, however, that would require you to download it from the server, modify it and upload it again if you are dealing with a server that has command line only. If your server has a visual editor like notepad, that’s a great solution. I just find it quicker to modify the file on the server in a few seconds.
Thanks for the input.
Cheers.