Script to display count of shell commands used most frequently

The credit for this excellent idea is to Matt Might. This perl script calculates the number of occurrencies of bash shell commands stored in ~/.bash_history

my %counts = () ;
while (my $cmd = <STDIN>) {
    chomp $cmd ;
    if (!$counts{$cmd}) {
        $counts{$cmd} = 1 ;
    } else {
        $counts{$cmd}++ ;
foreach $k (keys %counts) {
    my $count = $counts{$k} ;
    print "$count $k\n" ;

Save the above script in file “frequency” in some directory under “PATH” variable. cd to this directory and run chmod 755. After this, you should be able to execute the following command:

cat ~/.bash_history | frequency | sort -rn | head

Sample output may look like:

423 ls
6 make thesis.pdf
3 evince thesis.pdf
83 cd ..
55 make
43 hg status
30 make clean
25 cd src
24 vim Makefile
23 hg push

Note: in order to make it work, I had to remove “.” dot in the system call command in the “f” and in the “r” perl scripts!

This entry was posted in Bash, UNIX/Linux and tagged , , , . Bookmark the permalink.

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