Information about starting and operating an ISP or corporate Intranet using Linux servers.

Keep an (x)-day BASH History

Some helpful settings for BASH I have come to rely upon include:

  • Increasing the history line limit.
  • Storing history files by date to keep a 30-day command history for each user (or any number of days you choose).

Here is a sample file you can put in a place like /etc/profile.d/bash-settings.sh so that it will be automatically executed at each log-in.1)

#!/bin/bash
# Set BASH variables
# Called by /etc/bashrc, /etc/bash/bashrc, and/or /etc/profile
# Angelo Babudro ispltd.com

function fn_exists() {
   type $1 2>/dev/null | grep -q "function"
   }
fn_exists pathcheck
if [ $? -eq 0 ]; then
   pathcheck /usr/local/sbin
   if [ "$EUID" = "0" ]; then
      pathcheck /sbin
      pathcheck /usr/sbin
      fi
   fi
suffix=`tty | cut -d"/" -f3- | sed -e s/"\/"/_/g`
HISTFILE=~/.bash_history_`date "+%Y-%m-%d"`_${suffix}
[ -z "$HISTFILESIZE" ] && HISTFILESIZE=3333; #|Set >2000 or it will truncate the file
[ $HISTFILESIZE -eq 2000 ] && return;        #|Have we already been here once?
HISTFILESIZE=2000
for oldhist in `find ~ -maxdepth 1 -name "\.bash_history*" -mtime +30`
        do
                rm $oldhist
        done
#
# Only declare history file to root, since it interferes with SSH log-ins but
# as long as root cannot log-in via ssh it doesn't matter.  If you permit
# root log-ins via ssh then the next three lines should be commented-out.
#
if [ "$SSH_TTY" ] && [ "$EUID" = "0" ]; then #|Only show if this is a root log-in shell
        echo "History ${HISTFILE}"
        fi

# Useful aliases

alias ls="ls -F --color=tty"
alias dir="ls -lF"
alias grep="grep -I --colour"
alias rsync='rsync -hau --log-format "%o %l     %f"'
alias diffcols="diff -ty --suppress-common-lines --ignore-all-space"
export YUM0=`grep -o "[[:digit:]].[[:digit:]]" /etc/redhat-release`

#_______________
# vim: ts=3:sw=3
1) With RHEL5 and the current Gentoo release, any files ending in ”.sh” and residing in /etc/profile.d/ are automatically sourced from /etc/profile and/or /etc/bash/bashrc and/or /etc/bashrc.
Navigation
Print/export
Toolbox