Article

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. 

work-731198_1280.jpg

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
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?
me.jpeg

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!

Want more like this?

Keep up-to-date with latest news from Inside the Agency using any of the following services

Advertisement

Digital Ocean

Chris's Reading List

www.thoughtworks.com

BFF @ SoundCloud

Our friends at SoundCloud embarked on a journey a few years ago that changed their system and moved it from a monolithic Rails application towards an ecosystem of microservices. Read more

The Drum

The Drum

Google'€™s AMP is bringing some much needed speed to mobile but the transition won'€™t be easy for publishers

A week after launching, the Google-led Accelerated Mobile Pages (AMP) initiative has raised more questions than answers for publishers... Read more

SitePoint

SitePoint

Quick Tip: How to Permanently Change SQL Mode in MySQL

MySQL 5.7 introduced some awkward changes for older codebases and tends to break apps. Here's how to temporarily (or permanently) lower the strictness level Read more

SitePoint

SitePoint

How to Build Your Own Dependency Injection Container

Andrew Carter builds a Dependency Injection container from scratch, showing you how that rocket science is really just fireworks strapped to lego figures! Read more

AuthorityLabs

AuthorityLabs

13 Reasons Your Organic Traffic is in Decline | It's Not a Penalty

The landscape of search engine optimization has changed drastically in the last 3-4 years. Even as it changes, a lot stays the same. It has been very challenging to see a multitude of friends and colleagues approach me with problems that often seem beyond repair with clients that they have worked with for years. Read more

TechCrunch

TechCrunch

WordPress Sites Now Support Google’s AMP To Make Mobile Pages Load Much Faster

Google has some big plans when it comes to making the web faster on your mobile phone. The company just added AMP-enabled pages in its mobile search results... Read more