WELCOME TO YAROSLAV`S LINUX PAGE
+ -=:Bash History Archiving.:=- +
 

You're using bash, aren't you? You probably love it, especially if you use bash_completion script... Ok - no more lyrics. I would like to keep track of what, when and where I've done something in the shell for the rest of my Linux life. It is a reasonable wish to have all of my activites logged, so in the future I could check what I did, how I did.

The first solution would be to set HISTSIZE to be very big, but then I don't know how well your bash would behave - I believe it tries to keep them all in memory. But I want to have my bash fast! So it must be accomplished in another way.

Following paragraphs describe the idea in general and are outdated in some aspects. If you want really to use it, you better just grab the most recent version of 50_infinite_history file which is part of my bash configuration kept under GIT, place it in your home and source it from the .bashrc. The steps for inputrc are optional now and are the same and mentioned in the file itself as well.
Thu Dec 21 17:48:37 EST 2006: Incorporated changes from Philippe Teuwen: per command time stamps. Also added my aliases: gbha{,a}
Thu Mar 24 09:05:24 EST 2005: Made use of bash trap facility to provide proper activation of archive_history on exit (why noone pointed me to this possibility earlier? he heh). Now you can use "Ctrl-D" to exit bash and don't need to adjust .bash_logout.
Thu Mar 10 14:24:39 EST 2005: I've changed the separating lines in the output file, so now we have also a user name
Fri Mar 11 09:50:51 EST 2005: Now it can store increments of the history if you work for too long in shell and don't want to close it or have history been lost. It also calls history -a so to store main bash history as well. Fixed bug which forbid it scp-ing on some hosts.

Lets use a big file ~/.bash_history.archive (separate from HISTFILE=~/.bash_history). And then on exit from each bash session lets append new history lines to it.

The first problem with this approach was: how to make bash to call this script on each exit? Sure if you exit it by typing 'exit' then you can alias 'exit' function but I use shortcut Ctrl-D for that and I could not find a way on how to reassign it to something but not builtin exit function.

So the 1st attempt was: to forbid Ctrl-D by

export IGNOREEOF=10

and define double Ctrl-X combination to call exit function.

But the right approach is to use exit tramp of bash, which is a perfect solution because it gets called regardless of the way you exit bash: Ctrl-D, exit, close xterm window.

trap 'archive_history' EXIT

Following step is to define a starting line in our bash_history so we can save only new lines and make sure that we append to a history file.

export CURBASHSTART=`grep -v "^[ \t]*$" $HISTFILE | wc -l | awk '{print $1}'`  CURBASHDATE=`date`
shopt -s cmdhist histappend

That is all we need at the bash start - now we know from which history line current (fresh) history begins. To save history add next piece to your ~/.bashrc.

archive_history()
{
    HISTORYOLD=${HISTFILE}.archive
    CURTIME=`date`
    CURTTY=`tty`
    if  [ x$HISTDUMPPED = x ]; then
      echo "#-${HOSTNAME}-- ${CURBASHDATE} - ${CURTIME} ($CURTTY) ----" >>   $HISTORYOLD
      history $(($HISTCMD-${CURBASHSTART-0})) | sed -e 's/^[ ]*[0-9][0-9]* [ ]*//g'  >> $HISTORYOLD
      export HISTDUMPPED=1
    fi
}

exit ()
{
   archive_history
   builtin exit
}

File ~/.inputrc should contain next lines to redefine exit shortcut to Ctrl-x x. On a first try you might feel that it is terrible shortcut, but then you get used to it - believe me.

$if Bash
# to exit through calling exit function which will archive the history
"\C-x\C-x": "exit\n"
# to dump history we have so far
"\C-x\C-w": "archive_history\n"
$endif

Also if your bash session is a login session and you exit by calling 'logout', then you might add next line to your ~/.bash_logout

archive_history

After all the mentioned actions are done you can find your ~/.bash_history.archive file containing sections like these

#-belka-- Sun Oct 12 21:52:13 EDT 2003 - Sun Oct 12 21:53:25 EDT 2003 (/dev/pts/13) ----
exit
aptitude
exit
#-washoe-- Sun Oct 12 18:03:16 EDT 2003 - Sun Oct 12 23:06:48 EDT 2003 (/dev/pts/3) ----
exit
cd progr/letters/resume/
e resume.tex

which in general can be read as usual history file if you wish. So enjoy

If you have any comments - I would really appreciate them. Just email me directly or leave them on Debian-Administration article page, where I shared my scriptie with public ;-)

GeoURL | StumbleUpon | ICQ:60653192