In Nublue Blog 16 Really Useful Shell Commands for Web Developers Posted by Stefan For me using the shell while developing is an indispensable powerful tool (either through Putty in Windows or using Terminal in Mac OSLinux). If you have a VPS or a dedicated server there are some great out of the box shell tools which can help you develop faster and smarter. Below is my top 16 list of shell commands (not in any order of preference) I use when developing. I often find myself using the shell rather than using ftp. Here is the list and reasons anyway! Before starting with this article This article assumes basic shell navigation knowledge. If you aren’t familiar with using the cd and su command I recommend doing some google / wikipedia research before experimenting with the commands below. Introduction Here is a few bits of information that will help with regard to this article and using the shell in general: Shell # ./ 1 # ./ This means the current directory you are in (which can be checked by using the pwd command) Shell # ../ 1 # ../ This means go down a directory from the one you are currently in e.g. Shell # cd ../myfolder 1 # cd ../myfolder The TAB key on your keyboard auto completes file and folder names typed in the shell. So if you are in a directory which has a file called myfile.php you could type myf and then tab and the shell will auto-complete it saving you valuable keystrokes :). Shell # * 1 # * The (wildcard) symbol for ‘everything’. So for example all files in a directory or all folders in a directory Shell # -R 1 # -R This particular option means recursive (go through all sub-folders and files from the present working directory) for alot of file modifcation commands so is noteable for that reason Shell # man commandname 1 # man commandname This always provides the full manual for a command when typed in the shell. Sometimes google or wikipedia can be a bit more accessible for useful command examples. Short Disclaimer Some of the commands listed below require use of the root login. As its obligitory to mention in linux related articles like this, be careful when using this login as not paying attention with commands like rm can result in you needing to pay your ISP to restore your server! Installing virtualbox on your computer and then installing linux without a GUI/Desktop is a great way to experiment without risking breaking anything if you’re not feeling confident. Jump to a Section grep – Search inside multiple files fast! nano – Easy to use powerful text editor pwd – Find out where you are on your server ls – List the content of directories tar – Extract and backup files wget – Download files to your server find – Find files based on their name rm – Delete files from your server mv – Move files on your server touch – Create emtpy files chmod – Change file permissions chown – Change file owner & group mkdir – Create directories cp – Copy files sed – Find & Replace unzip – Unzip files 1. grep Grep is probably my favourite command. Ever needed to find out which file a class is being used but there are hundreds of files in multiple directories? grep will find it (almost instantly in most situations)! Example use Navigate to the root directory of the server in Putty or Terminal and run the following command Shell cd /var/www/vhosts/mydomain.com/httpdocs 1 cd /var/www/vhosts/mydomain.com/httpdocs Shell grep –r “the string I want to search” ./ 1 grep –r “the string I want to search” ./ The above command will recursively search inside every file from the web space root and the files in all sub-directories. More info Type: Shell man grep 1 man grep in the shell for more example uses of the grep Command Jump Back To The Command Menu 2. nano Ok this one’s a bit of a cheat as nano doesn’t always come pre-installed on a vps or dedicated linux server usually, however it is my favourite text editors due to its simplicity, speed and easy search functionality (developers familiar with other text editors should be able to get going with it straight away). There is also a little know about syntax highlighting option although I’ve never used it. Example use As I mentioned it’s is often the case that nano isn’t pre-installed on your vps or dedicated server. To install it on one of our Centos/Redhat VPS or Dedicated servers you would do the following: Shell # su 1 # su Switch to the root user Shell yum install nano 1 yum install nano Install nano using Yum. Just say yes to the prompts and it installs in seconds, job done. Unzip the file myzipfile.zip into the current directory General Usage Shell nano myfile.php 1 nano myfile.php Open myfile.php with nano If you want to create a new file in Nano type the following: Shell nano mynewfile.txt 1 nano mynewfile.txt As mynewfile.txt doesn’t existing in the present working directory nano will create a new file called mynewfile.txt Once inside the nano program most commands are ctrl + ‘a key’. So common ones are Shell Ctrl + X 1 Ctrl + X Exit the program. It will prompt you to save if you haven’t saved the changes you’ve made Shell Ctrl + W 1 Ctrl + W Search for a string, obviously invaluable for editing code etc More info Shell man nano 1 man nano Type the above in the shell once the program is installed for the nano manual Jump Back To The Command Menu 3. pwd Stands for present working directory and lets you know where you are folder wise at the present time. Seriously helpful in preventing you doing something silly in the wrong directory (like rm –rf for example 🙂 ). Example use Shell pwd 1 pwd Just this type this at any time in the shell to be told which directory you’re currently in. More info The Wikipedia entry on pwd http://en.wikipedia.org/wiki/Pwd Shell man pwd 1 man pwd Jump Back To The Command Menu 4. ls Lists the files in the present working directory. Invaluable for seeing what is where and how big files are. Example use Shell ls -lash 1 ls -lash Shows all files in a folder including hidden files and shows file sizes in human readable form (KB, MB, GB etc) More info The Wikipedia entry on ls http://en.wikipedia.org/wiki/Ls Shell man ls 1 man ls Jump Back To The Command Menu 5. tar A very useful program for both extracting and creating backup archives of files. If you’ve just downloaded the latest WordPress version in tar.gz format tar is the tool to quickly extract the fileset and start working. Also, very useful and quick for creating a backup of your files. Example use Shell tar –xzf latest.tar.gz 1 tar –xzf latest.tar.gz Extract latest.tar.gz into the current directory Shell tar –xzfv latest.tar.gz 1 tar –xzfv latest.tar.gz Extract latest.tar.gz into the current directory and display what tar is doing as it uncompresses the file. Shell tar -cf archive.tar myDirectories/ 1 tar -cf archive.tar myDirectories/ Creates a complete backup called archive.tar of the myDirectories/ folder and all subfolders Shell tar -cf archive.tar * 1 tar -cf archive.tar * Creates a complete backup called archive.tar of the entire present working directory and all sub-directories More info Shell man tar 1 man tar Jump Back To The Command Menu 6. wget Essential for getting files from the web into your webspace. This command is very useful combined with tar and Firefoxes ‘copy link location.’ I’ll give an example with the popular blogging application wordpress Example use Visit http://wordpress.org/download/ and right click on the .tar.gz link and select “Copy Link Location” now go to Putty or Terminal and type wget followed by right clicking to paste the download url Shell wget http://wordpress.org/latest.tar.gz 1 wget http://wordpress.org/latest.tar.gz Downloads the latest wordpress fileset. You’d now want to use the tar command to open up the tar.gz archive into the folder you downloaded to (again checkable using pwd) Shell tar –xzf latest.tar.gz 1 tar –xzf latest.tar.gz This uncompresses the archive ready to install once you’ve chmodded the appropriate files More info The Wikipedia entry on wget http://en.wikipedia.org/wiki/Wget Shell man wget 1 man wget In the shell for more example uses of the wget Command Jump Back To The Command Menu 7. find Just like it says on the tin find finds files based on filename, and just like all unix shell tools does it fast . Example use Shell cd /www/vhosts/www/mydomain.com/httpdocs/ 1 cd /www/vhosts/www/mydomain.com/httpdocs/ Shell find –name myfile.php 1 find –name myfile.php Unlike grep this command is automatically recursive from the directory you are currently in (to check this see pwd) More info Type: Shell man find 1 man find In the shell for more example uses of the find Command Jump Back To The Command Menu 8. mv Need to move some files from one location to another in the shell? mv does what you need. It is often handy to use a wild card * with this command to move more than one file or the contents of a directory. Example use Shell mv /myfolder/folderA /myfolder/folder 1 mv /myfolder/folderA /myfolder/folder Shell mv /myfolder/* /anotherfolder 1 mv /myfolder/* /anotherfolder These are a couple of simple examples. There is much more that can be done with this command if you read the manual as mentioned below More info Type: Shell man mv 1 man mv In the shell for more example uses of the mv Command Jump Back To The Command Menu 9. touch Although not that obviously useful, touch creates a completely empty file of your choosing. Where it can come in handy is when a application needs a config.php creating before it can write its config data. Using touch and possibly chmod to make the file writable (see point 5) can achieve this very quickly. Example use Shell touch config.php 1 touch config.php Creates a new completely empty file called config.php. Good times! More info Type: Shell man touch 1 man touch In the shell for more example uses of the touch Command Jump Back To The Command Menu 10. chmod Even if you’ve not been in the shell before you’ve probably know what this one does. Chmod permissions can be set using any ftp client. The main benefit of using the shell to set file permissions is *speed*. This method takes seconds or less compared to chmoding a fileset using ftp and having to put a brew on while you wait. There are some strong arguments for the ftp method due to the whole brew thing, but hey I’m impatient :-). Example use Shell chmod 755 config.php 1 chmod 755 config.php Give the file config.php 755 chmod permissions Shell chmod –R 755 * 1 chmod –R 755 * Recursively chmod every file in the current directory and directory below it to 755 Shell chmod 755 myfolder/ 1 chmod 755 myfolder/ Change the permissions on just the myfolder directory. If you put a –R in there it will do all files and folders below that folder too as shown below Shell chmod –R 755 myfolder/ 1 chmod –R 755 myfolder/ More info The Wikipedia entry on CHMOD permissions http://en.wikipedia.org/wiki/Chmod Shell man chmod 1 man chmod In the shell for more example uses of the chmod Command Jump Back To The Command Menu 11. chown Ever had some files that you just can’t delete via ftp ?? This is where chown often comes in very handy. Systems like the e-commerce solution Magento and most CMS systems will create files (usually files uploaded via the CMS control panel) where the user and group permissions are set to user ‘apache’ and group ‘apache’. This means that as your ftp user and group don’t have permission to delete files that belong to apache so they then can’t be removed by ftp. If you do need to remove these files an easy fix is to update the permissions via the shell and this is where the chown command is very useful. With this one command you can change both a files user and group in 1 go. Example use Shell chown myuser:mygroup file.php 1 chown myuser:mygroup file.php Change the owner and group of file.php to user myuser and mygroup. If using NuBlue VPS or Dedicated hosting the above command would be (the rest of the examples relate to our hosting): Shell chown myftpusername:psacln file.php 1 chown myftpusername:psacln file.php Give the file file.php ftp user ownership allowing it to be removed via ftp Shell chmod 755 myfolder/ 1 chmod 755 myfolder/ Change the permissions on just the myfolder directory. If you put a –R in there it will do all files and folders below that folder too as shown below Shell chmod –R 755 myfolder/ 1 chmod –R 755 myfolder/ More info The Wikipedia entry on CHMOD permissions http://en.wikipedia.org/wiki/Chmod Shell man chown 1 man chown In the shell for more example uses of the chmod Command Jump Back To The Command Menu 12. mkdir Makes directories/folders in your webspace (or any where else for that matter), enough said. Example use Shell mkdir nameofmyfolder 1 mkdir nameofmyfolder Creates a directory called nameofmyfolder. Its always handy to use the pwd and ls commands before doing this to confirm you’re in the folder you think you’re in. Shell mkdir –p /myfolder/myfirstsubfolder/mysecondsubfolder 1 mkdir –p /myfolder/myfirstsubfolder/mysecondsubfolder Create the above folder hierarchy Shell mkdir folder1 folder2 folder3 1 mkdir folder1 folder2 folder3 Creates 3 directories called folder1 folder2 and folder3 in the current directory you’re in More info The Wikipedia entry on mkdir http://en.wikipedia.org/wiki/Mkdir Shell man mkdir 1 man mkdir In the shell for more example uses of the mkdir Command Jump Back To The Command Menu 13. cp Copies files, obviously very useful. Example use Shell cp orginalfile copyoforignafile 1 cp orginalfile copyoforignafile Copies originalfile and creates a new file called copyoforiginalfile Shell cp –R ./myfolder ./mynewfolder 1 cp –R ./myfolder ./mynewfolder Copy all files and folders within myfolder to mynewfolder Shell cp ./httpdocs/myfolder/mywebfiles/* /var/www/vhosts/myotherwebsite.com/httpdocs/myotherwebfiles/ 1 cp ./httpdocs/myfolder/mywebfiles/* /var/www/vhosts/myotherwebsite.com/httpdocs/myotherwebfiles/ Copies the entire contents of mywebfiles into the folder myotherwebfiles in another webspace More info The Wikipedia entry on cp http://en.wikipedia.org/wiki/Cp_unix Shell man cp 1 man cp In the shell for more example uses of the cp Command Jump Back To The Command Menu 14. sed Although it certainly doesn’t sound like it this is ‘Find and Replace’ linux style. As with other more complex shell commands it can be difficult to remember so its often handy to copy and paste a sample and adjust for your needs Example use Shell sed 's/original text/altered text/g' example.php 1 sed 's/original text/altered text/g' example.php Find the string “original text” in example.php and replace with the string “altered text” Shell sed 's/original text//g' example.php 1 sed 's/original text//g' example.php Delete the text ‘original text’ from the file example.php More info The Wikipedia entry on sed http://en.wikipedia.org/wiki/Sed Shell man sed 1 man sed In the shell for more example uses of the sed Command Jump Back To The Command Menu 15. rm Removes files and directories. NB Its possible to seriously damage/destroy your server/vps with this command always make sure you’re in the right directory using pwd before running it. Example use Shell rm file.php 1 rm file.php Remove the file file.php Shell rm –rf * 1 rm –rf * Remove everything in the current directory and all files and folders below it. The ‘f’ in –rf means force which suppresses any deletion warnings so again make sure its correct before you do it as there is no undo. Shell rm –rf myfolder/ 1 rm –rf myfolder/ Remove the directory myfolder and all files and subfolders More info The Wikipedia entry on rm http://en.wikipedia.org/wiki/Rm_(Unix) Shell man rm 1 man rm Jump Back To The Command Menu 16. unzip Handy purely because sometimes a file that you might wget might be in a straight zip format rather than the common tar.gz compression. Example use Shell unzip myzipfile.zip 1 unzip myzipfile.zip Unzip the file myzipfile.zip into the current directory More info Shell man unzip 1 man unzip NuBlue is an award winning Web Design Company and a UK Web Hosting provider. Please get in touch and we will be happy to discuss your requirements.