How to get the top 10 recently used commands

The file .bash_history in a users home-directory saves the recently used commands. You can modify the maximum number of lines that will be stored in .bash_history. You only have to change the value of the global bash variable $HISTFILESIZE. For much more details have a look at the bash man-page.

We use sort, uniq and head to get the top 10 recently used command.

First we have to sort the complete file.
cat ~/.bash_history | sort

The second step is to count every command. For this purpose we use uniq -c. Now we know the number of occurrences of every command in the .bash_history.
cat ~/.bash_history | sort | uniq -c

The third step is to sort the output again but in reversed order. We use sort -rn for this purpose. The option -r means reversed order and -n is for numerical sort (the beginning of a line is the number of occurrences).
cat ~/.bash_history | sort | uniq -c | sort -rn

The last step is to get the first 10 lines. For this purpose we use head -n 10. Feel free to change the number, e.g. to get the top 100.
cat ~/.bash_history | sort | uniq -c | sort -rn | head -n 10
Example output:
91 clear
80 ls -l
10 rm *.dat
9 ls -l *.dat
8 git pull
8 bc -l
7 qgit
7 cd ..
6 man bc
6 git push


Posted on August 21, 2012, in Bash-Scripts and tagged , , . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: