![]() |
-=: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 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 Lets use a big file 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 So the 1st attempt was: to forbid 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 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 $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 archive_history After all the mentioned actions are done you can find your #-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 ;-) |
![]() |
||
![]() |
|
![]() |