WordPress Developer & Linux Administrator

Bash script to upgrade Wordpress

This bash shell script will accept input, confirming that you first have a valid WordPress backup file on hand. Once you’ve answered with a Y (indicating “yes”), the script will let you proceed to updating the WordPress core. The bash script pulls the newest version of WordPress from the official WordPress repository and places everything where it should be.

Installation of the WordPress upgrade script would look something like this:

cd /root/scripts
touch wordpress_upgrade
chmod u+x wordpress_upgrade
vi wordpress_upgrade

Just paste the WordPress upgrade script in there and run it.

Before running the script, you should first cd into the WordPress root directory for the website in question. Once there, you would execute the WordPress upgrade script like this:


That’s pretty much all there is to it! Once the WordPress files have been updated (ie., script finishing), you should login to the WordPress dashboard to verify if a WP database update is required. There often is some sort of database update, and you should do this soon so that WordPress works the way it is supposed to and your WordPress website doesn’t go down.

Particular Note

We use Boom Shadow’s fixperms.sh script (which we’ve recently modified and enhanced) to quickly repair permissions to user files on our systems. For our cPanel configuration, this is safe and works with our hosted WordPress sites. You’ll notice I’ve included a call to the script at the bottom. If you don’t want it there, just delete it. It helps automate tasks for us, though.

#  Date: Dec 11th 2014
#  Author: Will Ashworth (williamashworth.com || linuxscripts.org)
#  Bash script to upgrade WordPress core on an existing site
#  Copyright (C) 2015 Will Ashworth
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  GNU General Public License for more details. http://www.gnu.org/licenses/

# If using a version of bash earlier than 4.x you need to uncomment this
# shopt -s extglob

# Setup some vars
CURRENT_DIR_OWNER=`ls -ld . | awk '{print $3}'`

## -------------------------------------------------------------------------- ##

# Check to make sure we're actually in a WordPress directory
# ...and that a `wp-config.php` file exists
if [ ! -f "./wp-config.php" ]; then
    echo "A WordPress installation was not detected. Are you sure you're in a WordPress directory root?"
    read -p "Please confirm you have a good, working backup. Are you sure you wish to continue with this upgrade? Y/n " -n 1 -r
    echo    # (optional) move to a new line
    if [[ $REPLY =~ ^[Yy]$ ]]
        wget --no-check-certificate http://wordpress.org/latest.tar.gz
        if [ ! -f "./latest.tar.gz" ]; then
            echo "Oops! Something went wrong. We tried to fetch the latest copy of WordPress, but we couldn't get it! :("
            tar xfz latest.tar.gz
            rm -rf wp-admin/
            rm -rf wp-includes/
            cd wordpress
            rm -rf wp-content/
            mv * ../
            cd ../
            rm -rf wordpress/
            rm -f latest*.tar.gz

            echo "Fixing account permissions..."
            cd /root
            sh fixperms.sh -v -a $CURRENT_DIR_OWNER

        echo "Wordpress should be upgraded!"
        echo "You may need to login to your dashboard and run the database upgrade."


        echo "Exiting..."
Categories: Bash, WordPressTags:


Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.