notbanksy's blog

Web Design, Linux and other nonsense

Essential linux commands for web developers

This list is by no means exhaustive, but it should cover most eventualities for developers using linux. If you haven’t yet tried developing a website on a linux machine, then do so immediately! Thank you.

Some of the commands included here also appear in a previous post of mine – Top Linux Terminal Tips, so apologies to those obsessive followers (you know who you are!) for this small amount of repetition.  I should also credit ErisDS for prompting this artice – pop over to her fantastic musings of ErisDS blog; there’s some great stuff there. And apologies to Eris for not covering all her suggested topics… Way too much for one post, but watch this space!

Some Basics

So you’re staring at a flashing cursor in the linux terminal… The only thing you know about bash is that it’s something you want to do to your computer right now, and this blog represents the last thread of your self restraint!  Fear not, fellow developer, by the end of this tutorial you’ll be using your Windows recovery disks as coasters :p

Navigate the file system

Check out the prompt.  Chances are the prompt will be set to tell you the current directory, but you may just see this
ubuntu: ~username$

The ~ means home folder – the prompt may also show your username too, followed by $. Now if you type the following
ls

you’ll see a list of all the files and directories in the current location. However, if your current directory is your public_html folder, you’ll probably be cursing me already: ‘But where the *!&@’s the .htaccess file?!’  No problem:
ls -al

This outputs all files (a) including hidden files, as a long list (l).  In this view you’ll be able to see the permissions of each file and folder, and it looks something like this:
drwxr-xr-x+  5 JLSS  user     170 10 Jul 13:49 Public
drwxr-xr-x+  5 JLSS  user     170 10 Jul 13:49 Sites
-rw-r–r–@  1 JLSS  root  539018 10 Jul 21:13 my_picture.jpg

We’ll take a closer look at permissions later, so don’t worry about all this information for now.  Here are some more navigational commands:

Create a new file using the touch command – this is handy for creating a blank text document which can then be populated, for example .htaccess.
touch .htaccess – creates a new file called .htaccess in the current directory

Create a new folder in the same way using the mkdir command
mkdir new_folder – self explanatory

To browse the contents of your new directory, you need to change (working) directories (the location you are working in)
cd new_folder

Of course you can move to any directory in the filesystem with a single command if you know the exact path:
cd folder1/folder2/folder3/new_folder

to go to the root directory
cd /

Bear in mind linux always takes your command with the current directory as the point of reference.  To specify a location that can’t be explained in terms of your current location, you should describe the location in absolute terms, with the root directory as source:
cd /usr/bin

to go up one level:
cd ..

You want to move a file or a folder? Use mv:
mv filetomove.txt /new_folder

want to delete a file/ folder?
rm filetodelete.txt

if the folder to be deleted contains files also to be deleted you can specify a recursive delete:
rm -R folder

The wild card. Use * to specify any content you like, similar to how you may do so in SQL. This command
rm *.png

will remove all files containing the characters .png in that order. This would also remove a file called my.png_file

Gzip, zip & tarballs

You have a compressed file you want to extract? Here’s how.

Zip:
unzip file.zip

Gzip
gunzip file.gz

tar.gz / tar.bz2 etc
tar -xvf file.tar.gz / tar -xvf file.tar.bz2

You want to find a file by name – use find followed by the location to search in, then -name then the name of the file
find new_folder -name myfile.txt

and linux will show you the path to the files it finds like so
/home/user/new_folder/myfile.txt

You want to find a file which contains the word ‘foo’ in your home folder
grep -Hr ‘foo’ /home/user

Linux will output the file name and location.

Permissions

Remember at the beginning of this article, we typed ls -al to see a long list of files and folders in the current directory? And this is what we saw…
drwxr-xr-x+  5 JLSS  user     170 10 Jul 13:49 Public
drwxr-xr-x+  5 JLSS  user     170 10 Jul 13:49 Sites
-rw-r–r–@  1 JLSS  root  539018 10 Jul 21:13 my_picture.jpg

Here’s how is works and what you need to know. Those letters at the beginning of each line show you what permissions each object has, and this is how it works: the first character tells you what kind of object is there, the next three characters describe the users permissions, the next three are for group, and the last three are for guests/ others.
If the first letter is d, the entry describes a directory (folder), if it’s – then you have a file.
r means read, w is write, and x is execute, so a rw- permission means read and write access but not execute.  These permissions can also be described by a 3 digit number, which is how most *nix users deal with permissions, and here’s how it works.
r=4
w=2
x=1
You should add up the numbers of each of the 3 layers of permission (user, group, others) to make the three digit number, eg rwx=7 rw-=5 r–=4, so you could state that permissions in this case would be 754. Get it? Good!

Ok, the rest of the information is self explanatory – linux tells you the name of the file or folder and the owner – don’t worry about the rest, it’s not important for this article.

So, you want to change a file’s permissions? Easy! The chmod command does this, and it’s used like this:
chmod 755 yourfile.txt
So that will change the permissions to -rwxrw-rw-

Changing the file owner.
In linux, some actions can only be performed by the file/ folder owner, so the only way to get them to behave is to change the owner.  Use the chown command for this.
chown username filename.txt

If the file is currently owned by the root (administrative) user, you will need administrative permissions to make this change.  This can be achieved using the sudo command. Sudo means super user do.
sudo chmod username yourfile.txt

Linux will now ask you for the administrators password. Failure to produce it will mean the command is not executed.
Please note, not all linux systems accept the sudo command out of the box, or do not automatically assume that users can use it.  You can get around this using the su command.  It’s basically the same thing, except it begins an administrative session:
su
(linux asks for your password next)
chown username yourfile.txt

You will notice after typing the su command successfully that the prompt will turn from a $ to a #. This is a visual cue to let you know when you’re logged in as a superuser. When you no longer need these permissions, type exit to go back to the standard prompt.

Networking

Networking is a huge and complicated subject when considered from the linux terminal, so this is a quick start guide to common tasks and problems.  Please bear in mind that not all linux distributions are created equal or contain the same command line tools.  Not all of these commands will work on all distributions of linux.  If you want to see a list of all the tools installed on your linux machine, type cd /usr/bin and then ls (be prepared to see a very long list!)

Ping!
ping 127.0.0.1 – I do hope this is self explanatory – this also works if you specify a domain name instead of an IP address

Traceroute – trace the route to a remote server, you can use an IP address or domain name. This is great for identifying a bottleneck or other error somewhere in the route to your server
traceroute www.domain.com

Find the IP address of a domain
host www.domain.com

What’s my network IP?
route -n

Renew the DHCP lease to my machine
sudo dhclient
(linux will ask you for your password)

Display network information
ifconfig

change your IP address for a network adapter
ifcf eth0 del 192.168.0.3
ifcf eth0 add 192.168.0.2

Add a gateway
route add default gw 192.168.0.1

download a file from the internet
wget http://www.domain.com/filename.tgz

And finally, if you want to find out how to use any of the commands in linux, consult the manual:
man command

So there’s a web developers primer in linux.  It’s just scratching the surface, and doesn’t really convey the massive amount of fun you can have on a linux machine.  Something about web dev and linux just seem to go together – all my sites have been developed from start to finish using only linux (with the odd deviation for browser testing), and I highly recommend you give it a go.  It’s not until you force yourself to go entirely open source that you really begin to appreciate how expendable proprietary systems really are. And now that you know the lingo, think of how clever you’ll look in front of your friends!

And in other news, let it be known that a side effect of dog rescue is introducing kennel cough into your home. It’s so virulent I’m beginning to think even I have it… :(

Filed under: bash, linux

2 Responses

  1. Having just moved over to Jaunty (Ubuntu 9.04), I figured I’d re-read this.

    Very nice and informative article !b, and rest assured, I WILL be picking your brains in the VERY near future!

Leave a Reply

What’s notmrsbanksy up to?

Oh man! She's gone and set herself up as a dog rescue! Mostly saving pound dogs (UK) from being put down. Once Loved Dog Rescue.


She also lists UK pound dogs on her site Pound dog rescue link.


Boson has a new home!

Boson has a new home

WOOF!