John Trengrove

home posts about

Save your bash history to a local database with recent

16 Oct 2016

If like me, you spend a lot of time in the terminal, you have probably come across the limitations of the standard bash history file. The fact that the history of your commands is by default truncated to 500 entries, and that a session will only write to your history file after exiting makes it easy to lose information. Additionally, the lack of timestamps and other metadata is an issue.

I’ve created a small tool called recent that will solve these issues.

Recent does the following:

  1. It logs current localtime, command text, current pid, command return value, working directory to an SQLite database in ~/.recent.db.
  2. Logs history immediately, rather than at the close of the session.
  3. Provides a command called recent for searching logs.

By storing your command history into a SQLite database it becomes really easy to run more advance analysis of your bash history. For instance, questions like:

  1. What did I type last Wednesday in that folder?
  2. How many times do I commit code a month?
  3. What times am I most productive?

Can be answered. This lends itself to quantified self type, life logging. Patrick Bacon has kept a log of every command he has typed for three years (though his method doesn’t use a database like recent). It seems a great resource to have, as no human memory will retain this type of information over a period of years.

Installation and usage

If you would like to use recent, make sure you have sqlite installed, and then install via pip:

sudo pip install recent

And add the following to your .bashrc or .bash_profile to capture history:

export PROMPT_COMMAND='log-recent -r $? -c "$(history 1)" -p $$'

You can then search your history by typing recent [pattern]. I’m currently in the process of adding more functionality to this command like the ability to filter by date, working directory, and adding some aggregations.