DIY personal analytics

How many times a day do you reach for <ctrl> + r when using the shell? What about the history command? !! anyone?

Do we as programmers evolve and stop making the same mistakes? Do we really optimize our workflows? This is where the idea of personal analytics comes in. I am going to see what I can learn from looking at my bash history for the last few years. Here are the relevant settings in my .bashrc file:

shopt is a bash command that shows and changes shell option names. The histappend option tells bash to append the history collected to the filename specified in HISTFILE instead of overwriting the file. cmdhist tells bash to save all lines of a multiple-line command in the same history entry.

HISTFILE allows me to tell bash where and what to name history files. For example 2012-08-20.hist is today's bash history file.

Setting the prompt with history -a will append the current session history to an existing history file. This means your history file is updated as each command is executed on the command line, no waiting until the session is closed to write the history. I have found this helps with many terminals open at once and the need to share commands between them. The `history -n` part appends the history lines not already read from the history file to the current session history. These are lines appended to the history file since the beginning of the current bash session.

Getting basic statistics like how often I use a command or the last time I used it is easy. `ack -c ^perlcritic *` for example tells me how many times I called perlcritic and the filenames contain the date. The inherent problem is all the commands in the history files that are not helpful for whatever the task is at hand. So I wrote a simple filter script which creates a clean file and a file containing the junk removed. The initial files are not modified which allows this script to be used in an iterative manner.

Lets take a look at the code with most of the patterns removed just to show how short this program really is.

Now I have these cleaned up history files I can look at. I wrote this script last December and have been using it since to help identify commands that I use a great deal that are either long or hard to remember. For those commands I started making aliases and functions in my .bashrc so I wouldn't have to remember all the intricate details and to optimize my work flow with less typing and more getting things done. So far I have added over a dozen aliases/functions based on what my history has been telling me. Here are two Perl related ones:

# perl cleanup before git commit
pcl () {
  make clean
  rm Makefile.old
  cover --delete
  rm -r nytprof*
  rm smallprof.out
  rm -r fatlib*
  rm fatpacker.trace packlists

# build that perl coverage report
pcr () {
perl Makefile.PL
cover -delete
cover -summary -report html -test

I still dig through the clean files, add new patterns to the cleaner script and look at what I could automate away. This not only makes it easier for me to get things done but it is also a benchmark on the evolution of my tool usage.

I hope others try this out on their history files and see what interesting goodies you can dig up and possibly share with us.


This is a fantastic idea!

I've just implemented it myself. Looking forward to checking the results in a few months.

$ alias g
alias g='history|grep'

List history and grep out commands. For example, to list all commands with "perl":

$ g perl

Leave a comment

About Kimmel

user-pic I like writing Perl code and since most of it is open source I might as well talk about it too. @KirkKimmel on twitter