Home Articles Platform info Search-replace URLs using sed -i command

Search-replace URLs using sed -i command

There are several ways for replacing URLs when it comes to your site. But the simplest is using commands when you’re logged in via SSH. For WordPress sites we do have WP-CLI installed on all our servers which comes with a very handy command. You can find more information on that in our article about updating URLs in database.

But other content management systems might not have it that simple. There is an alternative method! 

Time needed: 5 minutes.

Search and replacing URLs via the command line can be done in with the following steps:

  1. First make a dump file of your database

    make a dump file of your database, preferably in your ~/private directory. Here’s the command for that:
    mysqldump --single-transaction -u DATABASE_USER -p DATABASE_NAME > ~/private/DB_DUMP.sql

  2. Make a copy of the database

    We recommend you make a copy of the file and make the changes to the new copy. This way you can always revert back:
    cp ~/private/DB_DUMP.sql ~/private/NEW_DB_DUMP.sql

  3. Search and Replace all matching strings

    Use the sed command to search and replace all matching strings within the DB dump file:
    sed -i 's+OLD-URL/NEW_URL+g' NEW_DB_DUMP.sql

  4. Import the new updated dump file

    As a last step, simply import the new updated dump file:
    mysql -u DATABASEUSER -p DATABASENAME < NEW_DB_DUMP.sql

Check that everything works. Remember that you might need to flush browser cache and cookies. If it breaks something you can always revert back to the original dump file you made!

Do you have any additional questions please contact our support team at servebolt.com!

Did this help you?