Shellisms: Keeping your history in check

I’m pretty obsessive when it comes to my shell history. I always want to be able to determine “what, where, and when” for any command I run. The following snippet contains all of my shell settings pertaining to history.

# Don't save commands leading with a whitespace, or duplicated commands
export HISTCONTROL=ignoreboth

# Enable huge history
export HISTFILESIZE=9999999999
export HISTSIZE=9999999999

# Ignore "ls" commands
export HISTIGNORE="ls"

# Save timestamp info for every command
export HISTTIMEFORMAT="[%Y-%m-%d - %H:%M:%S] "

# Dump the history file after every command
shopt -s histappend
export PROMPT_COMMAND="history -a;"

# Specific history file per host
HOSTNAME=`hostname -s || echo unknown`
export HISTFILE=$HOME/.history/$HOSTNAME

The “histappend” option and the PROMPT_COMMAND setting causes my history file to be appended after every command. This is handy as I often have multiple terminals opened on a single server. By default, history is fully read when the shell starts, and fully re-written when the shell exits. The default behavior can result in overwrites if you have multiple running shells.

My HISTFILE location creates an separate history file for each server I am on. This is really only useful if your home directory is shared, *or* you keep your .history folder in sync with periodic rsyncs.

Setting HISTTIMEFORMAT causes bash to timestamp all entries going in your history file. The file itself contains unix-style timestamps, but the “history” command will print humanoid-friendly timestamps:

$ history
15364  [2008-11-01 - 16:39:49] /opt/vmware/bin/vmware
15365  [2008-11-01 - 16:39:59] /opt/vmware/bin/vmware-config.pl
15366  [2008-11-01 - 16:40:02] su /opt/vmware/bin/vmware-config.pl
15367  [2008-11-01 - 16:40:04] sudop /opt/vmware/bin/vmware-config.pl
15368  [2008-11-01 - 16:40:07] sudo /opt/vmware/bin/vmware-config.pl
15369  [2008-11-01 - 16:40:53] cd /opt/vmware/
15370  [2008-11-01 - 16:40:57] ./bin/vmware-uninstall.pl 
15371  [2008-11-01 - 16:40:59] sudo ./bin/vmware-uninstall.pl 
15372  [2008-11-01 - 16:41:31] cd lib/
Share and Enjoy:
  • StumbleUpon
  • Facebook
  • Twitter
  • Reddit
Shellisms: Keeping your history in check

Leave a Reply

Your email address will not be published.