It's always a good idea to once in a while look at what commands you're using a lot and if there's any way of improving on them. Aliasing an often used command to a shorter command can save a lot of typing as can irregular but often used commands.

Using the unix history tool.

The first step is figuring out what commands we use. We can either use the history tool or examine the bash_history file. I'm going to use the history tool in this blog entry, because it's more general. You could also use it if you use zsh, terminator or whatever as your shell.


This outputs the recorded history with an enumeration first and the executed command second. The first commands recorded at the top and the most recent commands at the end. Notice that the final command in the history is "history". It will look something like this.

 [...] lots of lines like
 4851  vim .bash_history 
 4852  vim .bashrc 
 4853  wc .bash_history 
 4854  history
 4855  history | awk '{print $1}'
 4856  history | awk '{print $2}'
 4857  history

Discover what unix commands are used the most

The next step is parsing the output of the history command to show us what commands we use a lot. It is pretty easy with two nice unix tools sort and uniq. First we need to cut everything but the program used. For this I'm using awk. This is a bit of an overkill and the cut program could do this as well. I prefer to use awk though because it has more utility, so I use it more often. Cutting a specific column out is a pretty rare task, so I always have to consult the documentation for cut before I can use it. I don't need to do that for awk, so that makes it far easier for me to use.

history | awk '{print $2}' | sort | uniq -c | sort -n

This will return something like

 50 python
 51 vim
 58 cd
 58 g
 61 ssh
 92 sudo
128 ls
140 git

Second number in each column being the command used and the first being the number of times it was used. This is the output of my rarely used laptop. Comparing it to the output of my work PC could be really interesting.

I can see two things from this output. I have 'git' aliased as 'g', yet I apperently still use 'git' too much. I also use 'cd' a lot. But it should be a rarely used command. I have autojump installed and should be using that instead. Perhaps preventing myself from using this command by aliasing it to outputting a cheeky error message could be useful.