TechSmoke

May 2, 2007

One .bash_history for all your open terminals

Filed under: Tech — mike @ 12:55 pm

I’ve been wondering how to do this, but haven’t had a chance to search out a way to do it. Happened upon it in the LinuxJournal news letter.

BRAIN TRUST

This submission comes from Shashi in India:

As a research astronomer I work mostly at the command line in one or
more xterm windows at a time. Most of the time, data reduction jobs
are handled by writing simple one-line shell commands or
concatenations of various commands using pipes. Depending upon the
type of data being handled, mostly ascii catalogs, the commands can be
quite complex but still too simple to write full blown shell scripts.
To keep track of which sequence of commands were issued to arrive at a
given result I have included the following lines in my .bash_profile
to make per session:
HISTFILE=$HOME/.bash_history/bh_`echo $HOSTNAME | awk -F . ‘{print $1}’`.`date
+%d%b%Y.%I_%M%p`.pts`tty | awk -F / ‘{print $4}’`.log
HISTCONTROL=ignoreboth
HISTIGNORE=”f:df:w:dmesg”
HISTSIZE=2000
export HISTCONTROL HISTSIZE HISTFILE HISTIGNORE

My home directory is NFS mounted on various nodes of a cluster of
workstations, each with their own storage and processing capabilities.
At any given time I have quite a few xterm windows running on various
hosts running different data reduction and analysis routines. bash
uses the HISTFILE environment variable to save the session history. So
I came up with a solution that dynamically sets the HISTFILE
environment variable when bash is executed. The back-quoted commands
are executed first and their results concatenated to the environment
variable. For example, on the node “arches” a bash shell executed at
10:06pm on 19th of April, 2007 with an associated tty file of pts0
results in the following being used as the history file:
/home/shashi/.bash_history/bh_arches.19Apr2007.10_06PM.pts0.log

The HISTCONTROL environment variable is set to ‘ignoreboth’ which
means that consecutive duplicate command lines or command lines
beginning with a space character would not be saved. HISTIGNORE is set
to a colon separated list of commands that are not to be saved in the
history file. HISTSIZE is set to 2000 commands (default value is 500).

Locating the sequence of commands resulting in a particular output
file is then a simple matter of running grep on the history files for
the relevant filename. The grep can be speeded up by pruning the
.bash_history directory once in a while to remove irrelevant or
trivial history lists.

Leave a Reply

Powered by WordPress