> ## Documentation Index
> Fetch the complete documentation index at: https://kb.hosting.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Administering WordPress from the command line

> The WP-CLI tool let you do many WordPress administrative tasks from command line. You can also use it in cron jobs to automate tasks. Learn how here.

This article describes how to use the WP-CLI (WordPress Command Line Interface) tool to administer WordPress sites, as well as how to use it in cron jobs to automate tasks.

## About WP-CLI

It is often quicker and easier to do many tasks at the command line, and WordPress administration is no exception. You can use the WP-CLI (WordPress command-line interface) tool to back up and restore WordPress sites, install plugins, and much more. Additionally, you can use WP-CLI in cron jobs to automate tasks. WP-CLI provides similar functionality to the [Drush command-line tool for Drupal](/docs/using-drush-for-drupal).

<Tip>
  Because WP-CLI is a command-line tool, it helps if you already have some basic familiarity with the Linux command-line environment. If you have never worked in the Linux command-line environment before, you can learn the basics by reading [this article](/docs/introduction-linux-commands).
</Tip>

## Using WP-CLI at the command line

WP-CLI has many commands, and the rest of this article is only an introduction to its capabilities. However, to view WP-CLI's online help at any time, type the following command:

```bash theme={null}
wp help
```

Alternatively, to view the online help for a specific WP-CLI subcommand, add it to the **help** command. For example, to view the online help for the **wp plugin** command, type the following command:

```bash theme={null}
wp help plugin
```

<Note>
  You should run all of the following commands from the directory where WordPress is installed. Most of these commands will not work correctly if you run them in a directory that does not have a WordPress installation.
</Note>

### Configuring WordPress plugins

WP-CLI makes it easy to work with plugins. For example, to list all of the plugins that are currently installed on your site, type the following command:

```bash theme={null}
wp plugin list
```

To search for all plugins relating to a particular term, you can use the **search** option. For example, to search for all plugins related to SEO (search engine optimization), type the following command:

```bash theme={null}
wp plugin search seo
```

Similarly, installing and activating a new plugin is much quicker than using the web administration interface. To do this, type the following command, replacing ***name*** with the name of the plugin that you want to install:

```bash theme={null}
wp plugin install name --activate
```

To update all of your plugins at once, type the following command:

```bash theme={null}
wp plugin update
```

<Tip>
  By running this command in a cron job, you can ensure that all of your plugins are kept up to date automatically.
</Tip>

### Updating WordPress

Updating WordPress is simple with WP-CLI. To do this, type the following command:

```bash theme={null}
wp core update
```

Whenever you update WordPress, you should also update the database. To do this, type the following command:

```bash theme={null}
wp core update-db
```

To verify the WordPress version that your site is running, you can type the following command:

```bash theme={null}
wp core version
```

### Backing up and restoring a WordPress database

It is a good idea to periodically back up your WordPress database. By combining WP-CLI's backup functionality with a cron job, you can automatically back up your database on a set schedule.

To back up a WordPress database using WP-CLI, type the following command:

```bash theme={null}
wp db export
```

When this command finishes, you have a.sql file that you can store in a safe location. If you need to import a database into WordPress at some point, type the following command. Replace ***filename*** with the name of the database backup file:

```bash theme={null}
wp db import filename.sql
```

<Warning>
  **️ Warning**

  Be careful! This command completely overwrites the existing WordPress database with the database contained in the backup file.
</Warning>

### Optimizing and repairing a WordPress database

In addition to backing up and restoring WordPress databases, you can optimize and repair them. By periodically optimizing databases, you can help ensure that your WordPress sites run efficiently. To do this, type the following command:

```bash theme={null}
wp db optimize
```

Additionally, if you encounter MySQL errors when running WordPress, you can use WP-CLI to try and repair the database tables. To do this, type the following command:

```bash theme={null}
wp db repair
```

<Note>
  This command only works on database tables that use the MyISAM storage engine. It does not work on InnoDB tables. For more information about MySQL database storage engines, please see [this article](/docs/working-with-mysql-database-engines).
</Note>

### Backing up and restoring WordPress content

In addition to backing up and restoring databases with the **wp db** command, you can back up and restore site content. WP-CLI exports site content, including posts, pages, comments, and categories, to a WXR (**W** ordPress e **X** tended **R** SS) file.

To back up a WordPress site's content, type the following command:

```bash theme={null}
wp export
```

When this command finishes, you have a.xml file that you can store in a safe location. If you need to import a WXR file into WordPress at some point, type the following command. Replace ***filename*** with the name of the backup file:

```bash theme={null}
wp import filename.xml --authors=create
```

<Note>
  To import WXR files, you must have the WordPress Importer plugin installed and activated.
</Note>

### Changing a user password

You can use WP-CLI to quickly and easily change a user's password (for example, if you forget the administrator password). To do this, type the following command. Replace ***username*** with the name of the user (for example, *admin* ), and ***password*** with the new password:

```bash theme={null}
wp user update username --user_pass=password
```

<Note>
  * To view a list of all users on the WordPress site, type the following command:

    ```bash theme={null}
    wp user list
    ```

  * When you change a password, WP-CLI sends a notification message to the e-mail address associated with the user account.
</Note>

### Managing WordPress options

WP-CLI provides an easy way to manage WordPress options. For example, to change the admin e-mail address, type the following command, replacing ***[user@example.com](mailto:user@example.com)*** with the e-mail address you want to use:

```bash theme={null}
wp option update admin_email user@example.com
```

If you have set a custom login URL and need to retrieve this information, type the following command:

```bash theme={null}
wp option get rwl_page
```

## Using WP-CLI in a cron job

By combining WP-CLI's functionality with cron jobs, you can easily and automatically do administrative tasks that from the WordPress administration interface are time consuming and repetitive.

For example, the following cron line demonstrates how to back up a WordPress site every day at 2:15 AM using WP-CLI:

```
15 2 * * * cd /home/username/public_html; /usr/bin/wp export >/dev/null 2>&1
```

Similarly, the following cron line demonstrates how to automatically check for and install any available WordPress updates every day at 3:15 AM:

```
15 3 * * * cd /home/username/public_html; /usr/bin/wp core update >/dev/null 2>&1
```

## More information

For more information about WP-CLI, please visit [http://wp-cli.org](http://wp-cli.org).

## Related articles

* [Cron jobs](/docs/cron-jobs)

* [Run PHP scripts from cron jobs](/docs/run-php-scripts-from-cron-jobs)
