CuteMachine

Most Used Terminal Commands

I wanted to write an article about useful Unix commands, therefore, I wanted to see which commands I use most often for my daily work. To find these commands I needed to create a top 10 of the entries in my command history.

But how?

In the terminal, you can view commands you used in the past with the built-in shell command history. If you are using the zsh shell you have to add the parameter 1 to list the complete history:

# with zsh
history 1
# with bash
history

You will get a listing which looks something like this:

1  vi ~/.cntlm.conf
2  git diff HEAD^^..HEAD package.json
3  gd 0b54962 HEAD package.json
4  gd 0b54962^ 0b54962 package.json
5  git log -- package.json
6  git log package.json
7  docker run -d dockersamples/static-site
8  docker stop 0f6f36b15779
9  docker rm 0f6f36b15779
10  gcm 'Add welcome box for showcase'

But we are only interested in the actual command, not in the line number in the history file or the command parameters.

Therefore we pipe the output from history 1 to awk to filter out only the second column.

history 1 | awk '{print $2}'

Now we get the following output:

vi
git
gd
gd
git
git
docker
docker
docker
gcm

We can calculate the number of times a command appears in the history file by sorting the previous result and counting each appearance with uniq -c. That's why we have sorted the output before piping it to uniq. But since we only want to have the top 10, we also apply the command head -n 10.

This is the complete statement that will list your top 10 unix commands.

history 1 | awk '{print $2}' | sort | uniq -c | sort -nr | head -n 10

The below is my top generate by the above statement:

1809 ag
1342 gcm
1138 gcmb
 480 git
 477 gco
 447 npm
 351 cd
 320 nr
 314 gbc
 305 vi

ag - The Silver Searcher. Like ack, but faster
gcm - alias for git ci -m
gcmb - alias for git bc
git - the stupid content tracker
gco - alias for git co (checkout)
npm - javascript package manager
cd - builtin zsh command to change directory
nr - alias for npm run
gbc - alias for git checkout -b (now I use gnb - git new branch more often)
vi - alias for VIM - Vi IMproved

The result is not very surprising, though. I mostly write with vi, search code with ag, and commit my work with git. All-day long :)

Bonus Tricks

If you prepend a command with a space, it will not appear in your command line history file. This is a great trick to know when you work with secrets or passwords.

Another great trick is to increase the commands that will be stored in your command history file. I have configured my shell to store the last 10000 typed commands. You can do this in your .bashrc or .zshrc file by adding a line with export HISTSIZE=10000.

Posted on CuteMachine.

Jo's Profile ImageWritten by Jo who lives and works in Frankfurt building digital doodah. Stalk him on Twitter.

TOS • Privacy • Disclaimer