When you use SSH with WordPress, it can be one of the most secure and convenient methods of accessing your website.
SSH is a protocol that helps to access the files on your site which means ‘Secure Shell’. You can use it to access your site from wherever you are if you have your login credentials but make sure you are doing this securely.
This post will help you learn about some of the most useful SSH commands to use with WordPress.
Let’s get started!
What Is SSH?
SSH, also called as ‘Secure Shell’, is a protocol that can be used to access files and directories on a remote server.
You can use either the Terminal (on macOS or Linux) or a client such as PuTTY (on Windows) for accessing your site over SSH instead of using your browser.
How to Use SSH with WordPress?
An SSH will enable the access to the WordPress files themselves along with any files that have been added to the wp-content directory, either via the admin screens or directly.
You will be using it in mostly the wp-content directory and sometimes in the root directory to access the files such as wp-config.php.
For working with your site with SSH, open the Terminal or a client like PuTTY and access the server. You will need to insert the below information for this:
- Server address.
- Username.
- Password.
- Port.
What is PuTTY and How to Use It?
In MacOS or Linux, there is already an interface required to access your WordPress site via SSH. So, simply use the built-in Terminal to use SSH.
But in terms of Windows, it is needed to install a client for using SSH.
PuTTY is the most popular SSH client. Prior to accessing your site from Windows, you need to download and install it. Then use the same SSH commands in PuTTY as you used in Terminal.
How Can I Automate SSH Commands with WordPress?
When you use SSH to access your WordPress site, you can automate commands that you use frequently.
Use WP-CLI or the WordPress command-line interface to do this. With this interface, you can get an access to a library of commands specifically related to WordPress that can be used over and above those offered by SSH.
WP-CLI helps to automate WordPress management tasks such as installing plugins, updating your WordPress installation and configuring remote servers. The command line helps in enhancing the speed of the process and can help you become an highly efficient developer.
Benefits of Using SSH with WordPress
When you use SSH with WordPress, you get a number of benefits:
Security: It is possible to access your server remotely from any location until you are doing this on secure interface.
Convenience: You can access the server from anywhere you happen to be. There is no need of accessing the WordPress admin screens or even having a web browser running.
Speed: SSH commands can be much fast as compared to the WordPress admin screen or using SFTP to access files in many cases.
Since you now know about SSH is and the way it works with WordPress, check the most popular SSH commands that can be used with WordPress.
26 SSH Commands to Use with WordPress
The SSH protocol comes with a wide range of commands. Check below the list of the most commonly used commands divided into categories for convenience.
- Directory Commands
- File or Directory Commands
- File Commands
- Terminal Commands
- Other Useful Commands When Working via SSH
Directory Commands
You will find a range of SSH commands that can be used for working with or manipulating the directories (or folders) in your WordPress site.
pwd – Displays Current Directory (Full Path to Your Current Location)
With the pwd command, the full path will be displayed that displays your current location in your server. When you type this while accessing your site for the first time, it’ll tell help you know which server you are on.
This command will help to move further into your folders, and you will see extra information about your exact location. With this you can check your location, because there is no visual representation that you would get while using a code editor or FTP client.
ls – Displays Directory Contents (List the File Names)
With the ls command, the contents of the current directory will get displayed.
It is possible to use additional parameters for customizing or adding the information that the ls command returns:
- ls –a: showcases the hidden files.
- ls –l: displays the file details for directory contents, for example, ownership, permissions, date, etc.
- ls –s: shows the listing of files’ sizes in blocks.
- ls -s –h: displays the size in a form that humans can read.
Ensure that you type a space between the command and any parameters.
cd – Changes Directory
With the cd command, you will be able to move to next directory. It is possible to move into subdirectories of the current directory or move up or across with the correct parameters.
The command below will take you from the root directory of your server to the public directory:
cd public
For moving up the directory tree or going back where you came from, using one of these parameters:
- cd .. it takes you to the directory immediately above your current location.
- cd ../ the foldername will move you sideways into another folder within the folder right above you.
- cd – takes you to the previous directory.
- cd / takes you to the root directory.
The below screenshot displays that you have been moved into the styles directory and listed out its contents. In case you have an experience with the contents of a WordPress installation, it will further look familiar to you:
mkdir – Creates a New Directory
Using the mkdir command, it is possible to create a new directory in the current directory.
So, in case you are inside the plugin’s folder and you want to add a directory for stylesheet, you can use the below command:
mkdir styles
With this, a new empty directory to the folder will get added to the folder that you are currently in. For adding a directory to a folder lower down in the structure, you type in the structure. So, you can type the below command from the wp-content folder:
mkdir /plugins/myplugin/styles
rmdir – Removes Directory
With the rmdir command, a directory on your server will get deleted. Ensure that you take care while using this as it isn’t possible to undo it!
rmdir can be used with mkdir for removing a directory in your current location:
rmdir styles
Using rmdir you can remove a folder further down in the structure:
rmdir /plugins/myplugin/styles
Even rm can be used to remove directories, in case you add additional parameters.
- rm * foldername – delete all the files or content in a directory.
- rm -r foldername – delete the folder as well as the folders inside it.
File or Directory Commands
You can use the below command with either files or folders/directories.
cp – Copy File or Folder
Use the cp command with both files and folders for copying them to another location. You’ll need to mention the location of the file or folder to be copied to.
cp filename.extension /dir/
With this, the name of the file to be copied (including its extension) and the location it will be copied to will get specified.
For renaming the file (i.e. the new version will have different name from the original), then you can also mention the new file name.
cp filename.extension /dir/filename.extension
Using this command you can use a number of parameters as below:
- cp –r: it helps in copying all the contents of a folder.
- cp –f: using this the destination file can be deleted, if there is already another file having the same name, and overrides it with the new one.
- cp –i: warns you before copying.
- cp –u: it helps in overwriting the file in the destination folder, only if the files comprise of different content.
- cp –n: it ensures if the file already exists first and if so, it won’t copy. There isn’t any overwriting of the file.
- cp –a: helps to archive the original file.
mv – Move File or Folder
Along with copying files and folders, it is also possible to move them. This can be done using the mv command.
Using cp, you can either move a file or folder and keep the same name or move it and rename it.
- mv filename.extension /dir/ helps in moving a file and keeps the same name.
- mv filename.extension /dir/filename1.extension helps in moving and renaming the file.
- mv filename/ .. helps in moving a file one folder up.
zip – Compress File or Folder
You can use zip command with folders or files which can be implemented for compressing a folder and its contents.
It is required to include the name of the file/folder that will be compressed as a zip file. It can be used with parameters that will specify whether you want to delete the original folder or not.
- zip -r filename.zip foldername: helps in compressing a folder but doesn’t delete it.
- zip -m filename.zip foldername: helps in compressing a folder and deletes it. You will have just the zipped version of the folder.
- zip -d filename.zip foldername: helps in deleting a file from the existing zip archive that you specify.
- zip -u filename.zip foldername: It helps in updating a file within an existing zip archive: it can be useful if the original file is updated and don’t want to delete the zip file and create a new one.
While listing out the contents of the folder, you will see the zip file and the original folder if you used zip -r, but if you used zip –m, you will just see the new zip file.
unzip – Decompress a Folder
With the unzip command, a zip file will get decompressed in your directory and its contents will get extracted for creating the folder or files from which it was originally compressed:
unzip twentyseventeen.zip
If you haven’t deleted the original folder don’t use this command when zipping it. This is because the original folder won’t be overwritten with it. You can use zip -m to delete the original folder when you want to zip it, or use rm to delete it prior to unzipping the zip file.
Parameters can be added to unzip to customize its use:
- unzip filename.zip -x excludedfile.zip: This will exclude a file and ensure that it doesn’t gets unzipped with the rest.
- unzip filename1.zip filename2.zip filename3.zip: This will unzip multiple files. It can be used with as many zip files as you want, and the process is faster as compared to doing this in an FTP client.
tar – Create and Unpack Compressed Archives
The tar command allows you to create and unpack .tar.gz archives, which is an alternate option to using .zip archives. The same command can be used to compress and decompress files or folders.
Use tar cvzf for compressing a file or folder. To compress a folder, you can use the below syntax:
tar cvzf archivename.tar.gz directoryname
The archivename represents the name of the archive file to be created and directoryname represents the name of the folder to be compressed.
Use tar xvzf for decompressing the folder as below:
tar xvzf archivename.tar.gz
The four letters represent the below things:
- c informs tar to compress files.
- x informs tar to extract files.
- v means verbose and informs tar to display the names of files affected by the command.
- z informs tar to uncompress the archive.
- f informs tar you are supplying the name of the archive.
File Commands
The most common SSH commands specifically for use with files are as below:
touch – Create a New File
The touch command is used to create a new empty file on your server as below:
touch style.css
The file can be created only in the current directory and so, you will need to navigate to it prior to using the touch command.
cat – Show File Contents
The cat command helps to display the file’s contents.
The complete contents of any file will be displayed within the Terminal or PuTTY.
grep – Search for a Specific Phrase
In case the file you need to examine is long and the contents you’re looking for, use the grep command to search for specific content.
In the below example, a search is done for the word ‘asp_tags’ in the php.ini file in a theme:
With this command, all the lines containing the search term will get displayed
If you want to search for a string containing more than one word, enclose it in speech marks:
grep "nav class" footer.php
The grep command also comprises of multiple parameters that can be used to customize your search:
- grep -i “string” : It searches for a string case-insensitively in the file.
- grep -c “string” : It counts the number of instances of a string in the file.
- grep -l “string” * : It lists the files containing the string.
- grep -n “string” : It displays the line number(s) along with the result.
head – Read the First 10 Lines of a File
To simply read the opening lines of a file, use the head command as below:
head filename.extension
tail – Read the Last 10 Lines of a File
The tail command and the head command work in the same way, but the tail command reads the last ten lines of a file:
tail filename.extension
find – Search Files and Directories
With the find command, you can search through files and directories and return those that fulfill the criteria that you specify. You can search the current directory for all files whose name includes ‘index’ using the below command:
find . -name “index”
The syntax is:
find operator criterion string
You can use the below operators:
- / (slash) helps to search the whole system.
- . (dot) helps to search the working directory.
- ~ (tilde) helps to search the home directory.
So, if the above command is changed to this, str would search the whole system as well as the current directory:
find / -name “index”
The criteria you can use comprise of:
- -name – Represents the name of file.
- -user – Represents files belonging to a given user.
- -size – Represents files of a given size.
- -type -d – Represents the directory type. i.e. only search directories.
- -type -f – Represents the file type, i.e only search files.
The search term can be any string that you want to use. But make sure you enclose it in speech marks.
du – Get File Size
To check the size of one or more files in your WordPress installation, use the du (Disk Usage) command as below:
du -h wp-config.php
Here -h represents the disk usage to be displayed in human-readable format, instead of blocks.
You can also use the below extra parameters:
- df –h: The result will get displayed in a human-readable format.
- df –m: The result will get displayed in MB.
- df –k: The result will get displayed in KB.
- df –T: This will display the file system type in an extra column.
- df -ht /directoryname: It will enable you to view information about a specific directory.
- df help: It lists other options that can be used with their descriptions.
chmod – Change a File’s Permissions
With the chmod command, you can change a file’s permissions:
chmod 464 filename.extension
In order to find which permissions to use, use the Unix Permissions Calculator and copy the number you get into the command.
nano – Edit a File with the Nano Text Editor
In case you have created a new file using the touch command, or maybe the cat command for displaying the contents of an existing file, you might want to then edit the file.
This can be done using the nano text editor with the nano command as below:
nano filename.extension
You can edit your files right from the command line using this syntax.
It is very hard to undo your work as compared to using a code editor or a version control system. Just make minor changes, if you are confident about it.
Using the cursor keys along with the keyboard shortcuts listed at the bottom of the screen to navigate the file. Use Ctrl-X to exit the file and return to the Terminal.
vim – Edit a File with the Vim Text Editor
The vim text editor works similar to the nano editor and it can be accessed using the vim command. Which one to use, depends on personal preference:
vim filename.extension
wget – Download Files
Using the wget command you can download files from the internet. The files will get downloaded to the directory that you are currently in. It is needed to include the full path to the file that needs to be downloaded:
wget https://site.com/filename.ext
With this the filename.ext file will get downloaded from site.com and it can be stored in your current directory.
Terminal Commands
This category includes some of the most commonly used commands for helping you to work with the Terminal (or PuTTY) interface.
clear – Clear Screen
In case screen is cluttered, just type in clear to clear it and see yourself at the top of a blank screen again. To have a clean working environment or hiding your work if you’re in a public place use the below command:
reset – Remove Output from Terminal
With the reset command all of your commands will get removed along with the output from the terminal screen and will clear the screen.
history – Show last 50 used commands
The history command can be used to see your most recent task and repeat or undo something. It outputs a list of the most 50 recent commands used by you.
Other Useful Commands When Working via SSH
The below commands don’t fit under the headings but can be useful for working with your WordPress site via SSH:
netstat – Display Network Connections
Use the netstat command to see details of your network connection and status as below:
exit – Exit the Remote Server
After completing the work with your remote WordPress site, use the exit command to exit the server and return to your local machine.
This should be always done when you finish working, so no one else can access your server via SSH.
Summary
When you can access your WordPress site via SSH, you get a secure and convenient way to work with the files on your site without the need to open a FTP client or a web browser.
Fortunately the commands listed here help you to carry out most of the tasks required to do in WordPress over SSH and get more work done faster.