selective command history

Cameron Simpson cs at zip.com.au
Fri Sep 17 20:55:51 UTC 2010


On 17Sep2010 13:23, suvayu ali <fatkasuvayu+linux at gmail.com> wrote:
| On 17 September 2010 12:45, Andy Blanchard <zocalo at gmail.com> wrote:
| > Can you boil the commands that you don't want included in the history
| > file to a series of regular expressions such as the following:
| >
| >   ^cd
| >   ^ls
| >   ^rm
| >
| > If so, you can create a list of these regular expressions in a file,
| > then use the ~/.bash_logout script to clean up the history:
| 
| This is an excellent idea! I'll give this a shot. Coming up with the
| proper regex shouldn't be too difficult.

For myself, solving not-quite-your-problem, I contrive to log my
commands with a timestamp to an entirely separate history file
(~/var/log/shell), thus:

  : 20091119T115602; time ./backup-root-spare.sh
  : 20091119T115656; cd /opt

Just as many users have an alias "h=history" I also have small shell
function:

  hh()
  { grepall ${1+"$@"} <"$LOGDIR/shell"
  }

to search this file. (Grepall's a small script to look for lines
containing all words on the command line, so:

  [home/cameron]janus*> hh root spare
  : 20091119T115505; less backup-root-spare.sh
  : 20091119T115602; time ./backup-root-spare.sh

This means I don't care above preventing particular commands getting
into my history because I have an easy way to search the history
quite selectively.

The neat thing about that log format is that ":" is a legitimate shell
command, so I can cut/paste the whole line to reuse it instead of aiming
more precisely:-)

I'm doing this with zsh, which has a handy function it runs just before
every command is issued. I'd like to know if bash has similar, since I'm
force to use bash on some systems (notably recent Fedora, when my env
trips a memory issue in zsh somehow). Zsh runs preexec() before each
command, so I have:

  preexec()
  {
    loghistory "$1"
    ttyl "$1"
    last_cmdline=$1
    [ -n "$_slavefd" ] && toslave "$3"
  }

where loghistory is:

  loghistory()
  {
    _lh_line=": `date +%Y%m%dT%H%M%S`; $*"
    ##echo -E "$_lh_line" >>"$BASH_HISTFILE"      # give up trying to support bash's stupid history
    if [ -n "$LOGDIR" ] && [ -d "$LOGDIR/." ]
    then  echo -E "$_lh_line" >>"$LOGDIR/shell"
    fi
  }

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

You don't stop riding because you get old, you get old because you stop
riding.         - Anon. on rec.motorcycles


More information about the users mailing list