switch



16 Really Useful Shell Commands for Web Developers

May 5, 2010 at 3:54 pm, Category: Featured, Unix, Web Development, Web Hosting, by Tom

For me using the shell while developing is an indispensible powerful tool (either through Putty in Windows or using Terminal in Mac OS\Linux). 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.

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

# *

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

# -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

# 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.

  • 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

cd /var/www/vhosts/mydomain.com/httpdocs
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:

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. http://wiki.linuxhelp.net/index.php/Nano_Syntax_Highlighting

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:

# su

Switch to the root user

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

nano myfile.php

Open myfile.php with nano

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

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

Ctrl + X

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

Ctrl + W

Search for a string, obviously invaluable for editing code etc

More info

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

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

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

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

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

tar –xzf  latest.tar.gz

Extract latest.tar.gz into the current directory

tar –xzfv  latest.tar.gz

Extract latest.tar.gz into the current directory and display what tar is doing as it uncompresses the file.

tar -cf archive.tar myDirectories/

Creates a complete backup called archive.tar of the myDirectories/ folder and all subfolders

tar -cf archive.tar * 

Creates a complete backup called archive.tar of the entire present working directory and all sub-directories

More info

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

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)

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

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

cd /www/vhosts/www/mydomain.com/httpdocs/
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:

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

mv /myfolder/folderA /myfolder/folder
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:

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

touch config.php

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

More info

Type:

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

chmod 755 config.php

Give the file config.php 755 chmod permissions

chmod –R  755 *

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

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

chmod –R  755 myfolder/

More info

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

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

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

chown myftpusername:psacln file.php

Give the file file.php ftp user ownership allowing it to be removed via ftp

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

chmod –R  755 myfolder/

More info

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

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

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.

mkdir –p  /myfolder/myfirstsubfolder/mysecondsubfolder

Create the above folder hierarchy

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

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

cp orginalfile copyoforignafile

Copies originalfile and creates a new file called copyoforiginalfile

cp –R ./myfolder   ./mynewfolder

Copy all files and folders within myfolder to mynewfolder

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

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

sed 's/original text/altered text/g' example.php

Find the string “original text” in example.php and replace with the string “altered text”

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

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

rm file.php

Remove the file file.php

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.

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)

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

unzip myzipfile.zip

Unzip the file myzipfile.zip into the current directory

More info

man unzip

Our Client Line Up

Terms and Policies: Privacy Policy | Terms and Conditions | Environmental Policy | Equal Opportunities Policy | Sitemap
© NuFuture Ltd 2005-2010. Company No: 05523340  |  VAT No: 865 6930 80  |  InfoLab21, Lancaster, Lancashire, UK