Some command line tips for the web developer

Here are some command line tips that we think you may find useful

I wanted to share some tips I have collated over the years that would be useful for web developers who occasionally need to roll their sleeves up and get their hands dirty working on a server. These are by no stretch of the imagination a complete list and nor do I get too specific. The tips below should work on the majority of Linux based web servers and should apply to the majority of setups.

Where to get more help

The first thing I should point out is that if at any time you get stuck or need help you should consult your sysadmin, if you have no sysadmin and need to consult the internet, I would suggest browsing and then asking on ServerFault, the people on that site seem to know their stuff and it is a vibrant enough community.

Using Tab

When typing file or folder names you can tab once to automatically complete the name, this can vastly speed up your time in the terminal, so instead of typing vi /home/username/longFolderName/MyFileNameIsLongToo.txt you could type vi /h TAB u TAB l TAB M TAB.

You can also double tap the tab key to see a list of options available, this is useful if you have two filenames and the tab autocomplete doesn’t know what to do.

Listing Files

You may already know the ls command, but did you know that by adding -lah after it you can greatly improve the detail you get back from it.

-l lists everything out in a nicer format, -a includes hidden (dot) files in the list and -h makes thing like sizes human readable.

The other thing some people don’t know about ls is that you can pass in the location you want to look at, so instead of typing cd /home/user/ and then ls you can type ls /home/user/

Removing Files

Again most people know about using rm to remove files, but some don’t realise you can pass multiple files into it, for example rm file1 file2

Viewing Files

cat is your friend for quickly viewing the contents of a file, so you can just type cat my_file.txt instead of going into your text editor.

For larger files you might only want to see the very top of the file or the very bottom of the file, the quickest way to do that is to type head my_file.txt for the top or tail my_file.txt for the bottom of the file.

If you are constantly checking a debug file you can set up a quick command to constantly monitor it by typing tail -f my_file.txt this follows the file and it will automatically update as new stuff enters the file.

Drive Space

To know how much space is on your drives type df -h, you will normally get information for drives you didn’t think existed, don’t worry about it just focus on the ones that are clearly your main drives.

RAM

To know how much RAM is installed on your machine just type free -m, the main column you will want to worry about is the ‘total’ one.

History

Did you just get shouted at because you forgot to prefix your command with sudo? Type sudo !! and it will run the last command as sudo.

If you know you entered a command a couple of commands ago, hit up a couple of times and the terminal will add it in for you.

If you used a command a while ago and want to be able to remember it, type history and you will see everything that you have typed in the last while come up along with a unique number for it. Take note of the number and type ![the number] and the command will run.

The Pipe Character

The pipe character will take anything that would normally appear on the screen and passes it somewhere else, this is handy for basic searches. So instead of typing history and scanning for every time you used sudo, type history | grep sudo and you will get only items with sudo in it.

What is running

Find out what is currently running by typing top — pay attention to the load average, if it is high there may be something going wrong. Generally look for a full disk, a process that is going mental or a load of traffic to the site.

If you just care about the load average you can get it on one line by typing uptime.

Finding out More

man + any command will give you the manual for that command, super handy for finding out what you need without resorting to Google — also just like programs you run on your computer different versions could be installed on the server, this means that the man page will be more relevant sometimes than what google searches will tell you.

Moving folder contents up a level quickly

Move all contents of a folder into its parent with mv child_dir/* ./

This means take everything in child_dir (but not directory called child_dir) and move it to the folder we are in. If you don’t like the idea of typing ./ because it looks odd you can always type the full address.

Alias Commands

If there are long or complex commands that you type regularly you should alias them, this means creating another name you can reference the command by.

For example alias lsa='ls -lah' would allow you to type lsa and what would run would be ls -lah, you can even overwrite ls if you really wanted with alias ls='ls -lah'.

If you want to pass parameters you can create an alias with a function like this (just type it into the command line); mkdir_ls() { mkdir $*; cd $*;} then when you type mkdir_ls new_folder it will make a new folder and the move you into that folder.

In closing

Hopefully you find some of these useful, if you have any to add feel free to let me know by contacting me.


Recent posts View all

Ruby

Forcing a Rails database column to be not null

How you can force a table column to always have something in it with Rails

Writing Marketing

We've deleted an article's worth of unhelpful words

We've improved several pages across our site by removing words that add no value, and often detract from the article.