Skip to content

Instantly share code, notes, and snippets.

@petersenna
Last active October 3, 2019 15:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save petersenna/442f5f2ab97af65f24c0 to your computer and use it in GitHub Desktop.
Save petersenna/442f5f2ab97af65f24c0 to your computer and use it in GitHub Desktop.
Add this to your .bashrc to create a backup of your bash history every time you open a new terminal, but not more than one time per hour(find -mmin +60). This will backup your bash history if it has more lines than the backup file.
export HISTSIZE=""
HIST_FILE=~/.bash_history
BACK_FILE=~/.bash_history_backup
if [ ! -f $BACK_FILE ];then touch -d "2 hours ago" $BACK_FILE;fi
if test $(find $BACK_FILE -mmin +60); then
HIST_SIZE=$(cat $HIST_FILE|wc -l)
BACK_SIZE=$(cat $BACK_FILE|wc -l)
GROWTH=$(($HIST_SIZE - $BACK_SIZE))
if [ $GROWTH -lt 0 ];then
echo Looks like your bash history has problems...
echo You can restore with cp $BACK_FILE $HIST_FILE
else
cp $HIST_FILE $BACK_FILE
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment