In Nublue Blog

16 Really Useful Shell Commands for Web Developers

Stefan Posted by

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:

This means the current directory you are in (which can be checked by using the pwd command)

This means go down a directory from the one you are currently in e.g.

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 :).

The (wildcard) symbol for ‘everything’.  So for example all files in a directory or all folders in a directory

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

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

The above command will recursively search inside every file from the web space root and the files in all sub-directories.

More info

Type:

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:

Switch to the root user

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

Open myfile.php with nano

If you want to create a new file in Nano type the following:

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

Exit the program. It will prompt you to save if you haven’t saved the changes you’ve made

Search for a string, obviously invaluable for editing code etc

More info

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

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

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

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

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

Extract latest.tar.gz into the current directory
Extract latest.tar.gz into the current directory and display what tar is doing as it uncompresses the file.
Creates a complete backup called archive.tar of the myDirectories/ folder and all subfolders
Creates a complete backup called archive.tar of the entire present working directory and all sub-directories

More info

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

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)

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

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

Unlike grep this command is automatically recursive from the directory you are currently in (to check this see pwd)

More info

Type:

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

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:

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

Creates a new completely empty file called config.php. Good times!

More info

Type:

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

Give the file config.php 755 chmod permissions

Recursively chmod every file in the current directory and directory below it to 755

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

More info

The Wikipedia entry on CHMOD permissions http://en.wikipedia.org/wiki/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

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):
Give the file file.php ftp user ownership allowing it to be removed via ftp
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

More info

The Wikipedia entry on CHMOD permissions http://en.wikipedia.org/wiki/Chmod

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

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.
Create the above folder hierarchy
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

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

Copies originalfile and creates a new file called copyoforiginalfile
Copy all files and folders within myfolder to mynewfolder
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

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

Find the string “original text” in example.php and replace with the string “altered text”
Delete the text ‘original text’ from the file example.php

More info

The Wikipedia entry on sed http://en.wikipedia.org/wiki/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

Remove the file file.php
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.
Remove the directory myfolder and all files and subfolders

More info

The Wikipedia entry on rm  http://en.wikipedia.org/wiki/Rm_(Unix)

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

Unzip the file myzipfile.zip into the current directory

More info

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.

Stefan

Author Stefan

More posts by Stefan