My Top 10 Linux Commands

Over the last couple of years my role has expanded significantly and I have really had to pick up some good tips and tricks to try and make me as efficient as possible.  One area that has had to grow has been my familiarity with linux, terminal and my comfort with the command line and so I thought I would share my top 10 linux commands which I use on a regular basis. 


In no particular order:- 

1) Make a directory and any parent directories that do not exist.

mkdir -p app/cache

2) Replace a string within a file.

sed -i 's/alpha/beta/g' beta-site.conf

I found this useful for copying a vhost file from an existing site and quickly change all the references to the new domain name.

3) Check disk usage on server

df -h

When services such as MySQL don't start properly or you start getting cron error emails, this can sometimes be a symptom of a disk which is completely full. 

4) Run a command on a remote server 

ssh myserver uptime

Sometimes all I want is to run a command on a remote server but not actually enter a SSH session on the remote server.  My main requirement for this was to check the load on a server using uptime and as there were several servers which made up the platform I needed to create a small script which would run this command on each server so I could easily get a snapshot of the load across the platform.

There is a little extra configuration required to achieve this in an optimal way as SSH keys need set up so you can access the remote servers without constantly needing to enter the server username and password for each server.

5) Quickly find a phrase in a project folder when netbeans / eclipse is being slow

grep -r "the brown fox" .

I like Netbeans as an IDE but sometimes it can be unbearly slow, especially if it is scanning a project over the network so this little tip helps me to keep progressing when Netbeans is not playing ball.  If you are using sublime this can be achieved using Ctrl + Shift + F.

6) Run previous command as root

sudo !!

Does what it says on the tin!

7) Find the number of instances of a phrase within an archived error log 

less access.log.gz | grep "desktop=1" | wc -l

Logs can be tricky to analyse, especially if you are running a high traffic site.  This command was used to try and find how many instances were recorded for mobile users switching to the desktop version of the site.

8) Export database exluding stats tables

mysqldump -u username -p database --ignore-table=database.table1 --ignore-table=database.table2 > database.sql

A number of the sites I've worked on have had databases that are easily a couple of gigabytes so backing up and restoring these is a painful task.  Fortunately most of them have had sizable tables which can safely be ignored when backing up the database for transferring to a development server and this often results in a backup files of a couple of hundred megabytes rather than gigabytes.

9) Fix a accidental pull

git reset --hard HEAD
git clean -f

Ever accidentially pulled the wrong branch into your working directory.  The combination of these 2 commands helps straighten me out a few times.  The first reverts all the tracked files that have been modified to the latest commit and the 2nd removes any untracked files that are left lying around.  This has to be used with care as if your latest changes have not been committed then they will be lost, and if you have untracked files you want to keep you will need to make a copy of them or they will be lost too.

10) Run commands overnight without open connection

screen -R

This is great for running long processes overnight but without having to leave a SSH session logged in.  You can simply SSH to the remote server, run "screen" and then continue entering commands as you need.  When your process is running you can safely disconnect your SSH session or turn of your computer and the process will continue to run.  Later you can SSH back into the remote server and then run "screen -R" to re-attach to your screen session.

a) Search previously run commands

Press Ctrl + R

This one is not technically a command but it comes is so handy, not a day goes by that I don't use this one.  If you type in the phrase you are looking for and it doesn't show it right away then press Ctrl + R again and it will go to the next instance in the bash history it can find.

Now it is your turn

You've heard what helps me out so what command line tips or tricks do you have that helps make things that little bit easier?

I am a web developer specialising in web driven applications using PHP, MySQL, Symfony and Zend and I am currently working for the The Drum in Glasgow, Scotland.

Most days I can be found frantically coding away with EuroDance in my ears and consuming what I hope to be a never ending supply of coffee... happy days!

Chris's Reading List



How a single change boosted organic traffic by 40%

How a single change boosted organic traffic by 40%

The SEO world can sometimes be a confusing maze of contradictions. Read more

Sensible Interfaces

Sensible Interfaces

How did we get from 'Program to an interface, not to an implementation' to 'Just slap an interface on there, it's the fashionable thing to do'? Read more



The Linkbait Bump: How Viral Content Creates Long-Term Lift in Organic Traffic - Whiteboard Friday

The Linkbait Bump: How Viral Content Creates Long-Term Lift in Organic Traffic - Whiteboard Friday

A fantastic piece of content can lift your traffic curves to a "new normal." In today's Whiteboard Friday, Rand talks about exactly how that happens, and how you can create content that has that effect. Read more



6 Rules of thumb to build blazing fast web applications (server side)

6 Rules of thumb to build blazing fast web applications (server side)

In this post are described 6 extremely important rules you should take under consideration when writing your next web application Read more



Console Wars - PHP CLI Libraries

Console Wars - PHP CLI Libraries

Nicola Pietroluongo compares three popular PHP console libraries, building the same functionality in each. Wondering which one is right for you? Find out! Read more


TechCrunch Brings Its Bare-Metal Servers To The US Brings Its Bare-Metal Servers To The US, a hosting company with a focus on dedicated bare-metal servers that launched in Europe in 2005, today announced the opening of its first U.S... Read more


Digital Ocean