Bad code, bad humour and bad hair.

Archiving Bash History

Apr 23rd, 2009

This is a short one, but someone may find it useful.

Normally when you log out of bash it will write the history to ~/.bash_history. This is all well and good until you have multiple terminals open, because the last terminal to close will overwrite whatever is in ~/.bash_history.

I’ve written a little bit of bash code that I now put in my ~/.bash_logout file to take whatever is in the history buffer at logout and store it in a file something like ~/.bash_history_archive/2009/04/23/210400_12345.

1234567891011

PID=$$# DATE as YYYY/MM/DDDATE=`date +%Y/%m/%d`# TIME as HHMMSS in 24 hour timeTIME=`date +%H%M%S`HIST_DIR=$HOME/.bash_history_archives/$DATEHIST_FILE=$HIST_DIR/${TIME}_${PID}# create $HIST_DIR if it doesn't exist[[ -d $HIST_DIR]]|| mkdir -p $HIST_DIR# 'history -w' writes the current contents of history to a filehistory -w $HIST_FILE