SSH Commands Reference Compendium

Common SSH Commands Reference Compendium

Linux SSH Commands

SSH is one of the most powerful ways to control your servers and your data. Learning these ssh commands will make your life a lot easier both as a web master and as a developer or designer. I hope this guide has been helpful.

How to change directories

cd /directory/path

Changing and Exploring Directories

To look at the current directory, there are 3 commands you can type. Depending on the server configuration and Operating System not all will work.

 ls -al

Where am I?

Working with your server without graphics can take a little getting used to. If you ever need to see exactly which folder you’re in, use the following command:


It stands for print working directory. You’ll see output like this:


What’s here?

Next, we want to see a list of files and folders in our current directory:

ls -alh

ls is the list command, and -alh modifies the standard list in three ways. a means that all files, even hidden files, should be shown. l means that the long format is used – it shows things like file size and the date every file was last modified. h makes the sizes display in convenient units. Here’s some sample output:

drwxr-xr-x  3   15 Oct 21 10:01 .
drwxr-xr-x  6    6 Oct 21 09:13 ..
-rw-r--r--  1  137 Oct 21 10:01 .htaccess
drwxr-xr-x  2    4 Jun  8 17:24 errors
-rwxr-xr-x  1  379 Jan 28  2010
-rw-r--r--  1   45 Oct 30  2009 home.html
-rw-r--r--  1   83 Oct 21 09:47 index.php
-rw-r--r--  1   68 Jul 20 15:53 phpinfo.php

To move to the root directory, type:

cd /

Disk use

You will see output like this, with an amount used and a percent used:

Show all folder sizes for the current directory recursively, with their sizes:

du -h

If you run this from a high-level directory, it can take a while to complete.

Show a disk use summary for the current directory:

du -sh

Again, this can take a little while if you’re running it in a high-level directory.

Here’s an advanced find command you can run to find files over 10 MB (no variables in this one, just copy and paste):

(Just replace 00000 with your own site number, the rest works as-is):

find /home/00000/ -noleaf -type f -size +10000k 2>/dev/null -exec ls -lh {} \; | awk '{ print $5 ": " $9 }' |sort -n

If you want to return to the previous directory you were in before, just type:

cd -

To predict what you need to type - Finally the SSH terminal can predict what you’re going to type by pressing tab. So for example if you want to go to /var/www/vhosts/ but can’t remember which way round the www and vhosts were you could start by typing:


Then press the Tab key.

If there’s a file or folder starting with w (and not more that one of it) it will auto complete. So to complete the example above you would type:


This will return the path display as:


Looking at the example above you should have noticed two things. First was that tab autofilled the forward slashes(/) for at the end of each directory, thus shorting the number of characters I typed. Second was that I had to press Tab twice, first after the letter h then after the letter d. That was because within the directory. There were two folders the with similar names.

# public_html (the one I was after)

# public_htmls (very similar but with an s in there)

As a result the Tab key autofilled ‘http’ but didn’t know which of the two directories I was autofilling. Pressing d then Tab again confirmed that I wanted public_htmls and not public_html which would have required the letter ‘s’ instead as that’s the first letter after the autofilled item.

Copying Files

To copy a file with the same directory simply type:

cp filename-to-copy.txt new-file-name.txt

For example:

cp index.html index.back.html

To copy between directories

cp filename-to copy.txt ../../new-directory/filename-to-copy.txt

For example

cp contactus.php ../contact/contactus.php

To copy all files from one directory to another, use the * character, which unofficially means all:

cp images/* ../pics/

SSH Command Flags

Adding Flags to Commands

There are additional elements you can add to the command called Flags. These add extra properties to the command that might be necessary but don’t happen by default.

Flags are easily spotted as they generally appear after the command tag, in the case above ‘cp’ and always start with a hyphen ‘-’.

The most commonly used flag for copying files (cp) is the -a Flag. This will copy the file or directory across retaining the permissions whilst retaining the permissions and ownership:

cp -a contactus.php ../contact/index.php

Flags can also be daisy chained to add multiple properties to one command. A great example is the compression of a directory below using the .tar.gz method.

Moving Files

To move a file simply type:

mv current-directory/existingfile.txt ../new-directory/existingfile

For Example

mv images/header.jpg ../public_html/


The rsync command can be used instead of the cp command, and works on all platforms of linux. Rsync can be used in the same way as cp with added benefits such as file permission and ownership preservation, compression during transfer, and comparison between files for updates to back ups.

rsync -a logo.png images/logo.png
rsync -a index.php index.php_old

rsync is the command, followed by the ‘-a’ flag which lets the system know to preserve the permissions, ownership, timestamp, and if rsyncing a directory, to do it recursively. The next part, ‘logo.png’ is the source file followed by the destination for that file to be copied to.

Another use for rsync is the ability to copy files and folders over a network to another server. This can be very helpful if you want to run some back ups, or migrate from one server to another within the (mt) Media Temple network. This first example will show you how to copy the entire document root for to another server using it’s SSH login credentials.

rsync -avz user@host:/path/to/destination

When you hit enter, you will be prompted to enter in the password for user on the remote server. This will move the entire directory, and it’s contents over to the destination on the remote server. The options, ‘-avz’ let rsync know that you want to archive the file permissions, ownership, get a verbose readout of what file is being processed, and you would like to compress the files in order to use less bandwidth.

You can also reverse this to download a copy of files from a remote server to your local machine.

rsync -avz user@host:/path/to/source ./

Once again, you will be prompted for the password for the user account on the remote server. This will copy the specified file from the remote server to the current working directory on your local machine.

Renaming Files

To rename a file, use the ‘cp’ command as before, but change the name of the file when stating the directory receiving the file.

mv oldfilename.txt newfilename.txt

For Example

mv index.php index.bac.php

Note: You must delete the original file from the server as essentially you’re not renaming the original file. Instead you’re creating a copy of the original file with a different name.

Compressing Files with Zip

To create a zip file, simply type:

zip -r directoryname

For Example

zip -r public_html/*

Note: The hypen -r ensures that the file and directories within the parent directory being compressed are also included.

Extracting Zip Files

To unzip a file:


For Example:


Note: For simplicities sake, always place the .zip file in the directory you would like it’s contents to be unzipped to.

Compressing Files with tar.gz

To create a tar.gz file, simply type:

tar -cf archivename.tar.gz directory-or-file-to-archive/

For Example

tar -cf website-backup-2014-1-31.tar.gz public_html/

Note the Flags czvf, they stand for:

Compress – Creates the new archive.

Zip – Compresses the file.

File – Implies that we have given the compressed file a name.

Verbose – Prints what the command line is doing, like a progress report.

Extracting Files with tar.gz

tar -xzf archivename.tar.gz

For Example:

tar -xzf website-backup-2014-1-31.tar.gz

Note: To make it easy to find and extract your archive, always place the .tar.gz file in the directory you would like it’s contents to be unzipped to.

Backing up Databases

To backup a database via ssh, type:

mysqldump -u database_username -p database_name > name_of_backup.sql

For Example:

mysqldump -u wordpress_bob -p wordpress_blog > wordpress_blog_20140131.sql

Importing Databases / Restoring Backed up Databases

To restore and import a database you first need to create the bank database then assign a user. Using these details you must replace the database name and user below:

mysql -u database_username -p database_name < name_of_backup.sql

For Example:

mysql -u wordpress_bob -p wordpress_blog < wordpress_blog_2014-01-31.sql

Backing up Files and Folders

To backup files, either use .tar.gz or .zip methods above. (I recommend using tar.gz) Then download the data archive as local backup. I would always recommend compressing files using .tar.gz as opposed .zip as it results in much smaller file sizes. This makes a big difference when bandwith and download and upload times are important.

Note: Always test your backups. Backups can sometimes be incomplete or become corrupted. Test your backups and make sure they are valid and working and not missing any data.

Moving Servers with SSH

The is by far on the best features of SSH, but not many shared hosting providers allow it. (You should ask your hosting provider if they allow ssh on your server, and if so, have them activate it if it is not already.) Once you have created backup archives of the site directories and files, if you were using a regular FTP client, you would typically download the large .zip or .tar.gz file to your local computer, and then re-upload the file. Not anymore. Using SSH, you can actually download the file from the old server directly to the new server using wget command.

1) Create your backup on server1 then make sure it’s accessible through a web browser.

2) Login to server2 via SSH and navigate to the directory you wish to download the large back file to.

3)Then type the following ssh command:


For Example:


Changing File and Folder Ownership using CHOWN

To change just the owner of the file to the user ‘root’ type:

chown root filename.txt

To change the owner to root, but the group to ‘superadmin’ type:

chown root.superadmin filename.txt

To recursively change owner and group permissions for all files and folders within a directory (and all subdirectories and files) use the ‘-R’ Flag:

chown -R username.groupname filename.txt

Changing File and Folder Directory Permissions using CHMOD

CHMOD Overview CHMOD (Change Mode) is the file permission system used by UNIX based servers which Site5 uses. The permissions appear as a set of 3 numbers in a row. In order of appearance, the numbers represent user, group, and other’s access level.

Without going into too much detail, here are some basic examples of the most commonly used permissions.

755 = Can be viewed and executed by anyone, can only be modified by the user.
This is most commonly used on folders and can be used as a substitute for 777 on our servers.
644 = Can be viewed by anyone, but can only be changed by the user.
This would be used for php and other file types.
444 = Can be viewed by anyone, can’t be modified without changing the CHMOD.

This would be used for protecting configuration files.
Note: This permission can’t be set via FTP

Note: Another permission level that is mistakenly used often is 777. Using this permission level is very dangerous because it means that anyone can read, execute and change the file. In fact we have prevented files that have group or other write access from running for your security. Attempting to use permission 777, 757 or 775 will cause an error 500 page to appear when trying to access that file in a browser.

Login as a super user (root) via SSH

As a security measure, sometimes the superuser ‘root’ can only be used once you’ve logged in as a standard user. To change your user, type:

- su

then type your password.

Note: You can also execute any command as root by typing “sudo” before the command.

Deleting Files and Directories – rm ssh command

To delete a file:

rm filename-or-directory-to-delete.txt

Alternatively, if you wish to delete a directory, and all directories and files within that recursively, type:

rm -r

This should ask you for confirmation, but never assume when deleting files that you will be asked to confirm the action. Always make sure you’re deleting the correct file before running the command.

SSH Restarting MySQL Server

This is more for advanced users and should never be used in a shared hosting environment. To restart the MySQL server using SSH, type:

/etc/init.d/mysqld restart
 service mysql restart

Note: This is 2 separate commands and both need to be executed.

SSH Restarting Apache

This is more for advanced users and should never be used in a shared hosting environment.

To restart Apache gracefully (allowing current connections to complete first) using SSH, type:

apachectl graceful

To restart Apache immediately type:

apachectl restart

Show System Processes with SSH

To see what the system processes in a human readable way, type:

ps aux --forest

or you can use the top command:



Then to exit top type :


Find All Files containing a Phrase

To search for a phrase inside a site, type the following replacing ‘PHRASE’ with your phrase:

grep -H -r "PHRASE" * | grep -v filter | cut -d":" -f1 | sort -u

Find out current Max MySQL Connections

To work out how many active MySQL sessions you can have at any one time login to MySQL

mysql -uadmin -p`cat /etc/psa/.psa.shadow`

Then give this commands

show variables like 'max_connections';

Increase Max MySQL Connections (Plesk Server)

To Increase the Max MySQL Connections type, you first need to open up the my.cnf file

vi /etc/my.cnf

Then add the following line beneath the socket declaration


Note: This will increase the max number from 100 (generally the default) to 250.

Next type the following to save and close (remember to come out of insert mode in vi editor first)


Finally restart MySQL

/etc/init.d/mysqld restart

SSH Commands – A quick reference guide to ssh commands, linux shell commands, and Mac OSX Terminal Commands