Enhancing and Enlarging the Bash History

Commands you enter on the Linux command line are valuable. They are part of your computer knowledge, you should remember them. If you don't, [Ctrl]-[r] will come to your rescue to search your Bash history for commands you entered in the past. But your Bash is forgetful by default. Only the very last commands will be kept in the Bash history file ~/.bash_history. If you want it to remember more of your previously entered commands including preciously crafted arguments and parameters, add these lines to your ~/.bash_profile:

# Remember also the date&time of entering the command:
# ... you can test this with:
#     test with  history | more
# The size of the history should be set to a greater value:
# The Ubuntu Shell usually is set up to ignore duplicates.
# You may disable this to store every single executed command:
export HISTCONTROL=ignoredups

Mining data from your ~/.bash_history file

Some years ago I wrote a script to store the bash history in an SQL database. (You can find a link in the Resources section.)

Recently I rewrote it to read the Bash history into a Pandas DataFrame. I like this more for interactive data analysis and it comes without all the hassle and complexity of SQL (but yes, it depends on more than just the Python standard library). Here is the result:

hist_to_pandas.py – Read your Bash history into a Pandas DataFrame with this Python script.

Saving your Bash History in a Git Repository

At work (the university Frankfurt) we use the same account on all Linux computers. Some of them have problems to respect my HISTSIZE/HISTFILESIZE settings. Then all my older history gets lost. To prevent those things / be able to restore my history in case of trouble, I decided to store it in a Git repository.


My solution:

Creation of a git repo for the purpose:

mkdir ~/bash-history/
cd ~/bash-history/
git init

Creation of the script ~/back-up-bash-history.sh:

cd ~/bash-history/
cp ~/.bash_history ./bash_history
git add .
git commit -m "new version on $(date +'%Y-%m-%d_%H-%M-%S')"

And a crontab entry:

*/5 * * * * ~/back-up-bash-history.sh