12 essential Linux SSH commands for every developer

There are hundreds of commands in Linux (and Unix), but for web developers who setup and configure websites all day long, the following basic commands are probably the ones that are most used.

We’ve included different scenarios of how each command might be used – please let us know in the comments if there are any situations we haven’t covered properly.

If you’re a total beginner to this…

SSH (Secure SHell) is a protocol that allows you to login to a remote server and issue commands (with a DOS/Shell text interface). Using a command-line interface can make some tasks 10 times faster than doing them manually.

On a Windows computer, you can SSH using ‘Putty’ (you only need putty.exe). On a Mac, the ‘Terminal’ utility is already built into the OS.

cd – Go to a directory

Go up a directory :

cd..

Go to the ‘images’ folder (assumes the images folder is inside the current directory) :

cd images

Go to a specific folder (in this case we’ve gone right back to the server root) :

cd /var/www/vhosts/mywebsite.co.uk/httpdocs

ls – List files and folders

Output a simple list of all files and folders :

ls

Output an advanced list all the files and folders in a directory (showing permissions, owner, group, dates etc) :

ls -l

zip – Zip up files and folders

Zip up everything in the ‘forms’ folder (the -r flag zips up all sub-folders and files) into a new file call Dec2012FormsBackup.zip :

zip -r 20Dec2012FormsBackup.zip forms

Zip up everything in the current directory tree, apart from the ‘archive’ folder (the -x flag excludes folders) :

zip -r 26Dec2012Backup.zip * -x archive/\*

Zip up the contents of the httpdocs folder and put that zip file in another folder of the server (In this case we’re using the -v and -r flags together to suppress the output and recurse into the directory, and -9 flag to compress better) :

zip -vr -9 "/var/www/vhosts/destinationwebsite.co.uk/httpdocs/files.zip" "/var/www/vhosts/mywebsite.co.uk/httpdocs/*"

unzip – Unzip an archive

Unzip a .zip file into the current directory :

unzip 26Dec2012Backup.zip

cp – Copy files or folders

Copy an html file to another directory :

cp mypage.html /var/www/vhosts/mywebsite.org/httpdocs

Copy everything from websiteA’s httpdocs folder to websiteB’s :

cp -r /var/www/vhosts/websiteA.com/httpdocs/* /var/www/vhosts/websiteB.com/httpdocs

Copy everything in the current folder :

cp -r * /var/www/vhosts/website.com/httpdocs

Copy the ‘forms’ folder :

cp -r forms /var/www/vhosts/website.com/httpdocs

mv – Move files or folders

Move the ‘js’ folder into the ‘oldwebsite’ folder :

mv js oldwebsite

Move everything from the current directory into websiteA’s httpdocs folder :

mv * /var/www/vhosts/websiteA.com/httpdocs

rm – Remove (permanently delete) files or folders

Remove/delete a html file :

rm mywebpage.html

Remove/delete the ‘images’ folder and everything inside it (the -f flag removes prompting e.g. ‘are you sure?’ and -r recursively deletes sub-folders):

rm -f -r images

chmod – Change permissions

Change every file and sub-folder in the ‘pictures’ folder to ‘777’ (note that 777 is the most open permission on linux) :

chmod -R 777 pictures

Change everything in the current directory to 755 (note that 755 is the default permission level files are given when uploaded via FTP) :

chmod -R 755 *

chgrp – Change group

Change everything to to the group ‘servergrp’

chgrp -R servergrp *

Change everything in the ‘photos’ folder to the group ‘psaserv’

chgrp -R psaserv photos

chown – Change owner

Change the owner of the ‘httpdocs’ folder (and all sub-folders and files) to ‘admin’ :

chown -R admin httpdocs

Change everything in the current directory to the owner ‘website_user’ :

chown -R website_user *

mysqldump – Export MySQL database

Format:

mysqldump -a -u USERNAME -p DATABASE > FILENAME.sql

Export the ‘wordpress_db’ database into the ‘wp_data_02may13.sql’ file :

mysqldump -a -u wordpress_user -p wordpress_db > wp_data_02may13.sql

mysql – Import MySQL database

Format:

mysql -u USERNAME -p DATABASE < FILENAME.sql

Import the ‘wp_data_02may13.sql’ file into the ‘wordpress_db’ database (assuming you’ve already uploaded the .SQL file to your server via FTP) :

mysql -u wordpress_user -p wordpress_db < /var/www/vhosts/websiteA.com/httpdocs/wp_data_02may13.sql

Comments & Feedback

We'd love to hear what you think about this article - please keep in mind that comments are moderated and any spam or rude comments will be removed. Thank you!

2 responses to “12 essential Linux SSH commands for every developer”