Saturday, August 6, 2011

make a back up of the bash history

This function backup your bash history in a file named history.back when it reaches 900 lines. It reads the hidden file .bash_history in the $HOME directory counting the lines. If the count is greater than 900, appends the content of .bash_history into history.back and removes the file .bash_history. To run the script at boot (I'm using Fedora) I put the path of the script in /etc/rc.d/rc.local.

#!/bin/bash
#backup .bash_history

#Created by solde9
#on 2011-08-06

FILENAME=$HOME/.bash_history
count=0
HISTORY_BACK=$HOME/history.back

while read LINE
do
    let count++
done < $FILENAME

if [[ count -gt 900 ]]; then
    echo '===============================' >> $HISTORY_BACK
    cat $FILENAME >> $HISTORY_BACK
    rm $FILENAME
fi