My Top 10 Linux Commands
- November 3rd, 2014
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
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
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.