Warning: This is not an answer to the question as it stands, but rather a suggestion for an approach that you may find useful.
I feel your pain. I like to keep around my Bash history as well, and be able to recall commands that I might have run a long time ago. I have developed an approach that allows me to keep all of my Bash history within a reasonable history size.
my Bash history settings are:
That helps to keep down the entries that end up in the Bash history in the first place. Commands like
ls are shorter to type than to recall, so there's no point in storing them (that's what
HISTIGNORE is for).
ignoreboth is shorthand for
ignoredups. The former prevents entries with a leading space from ending up in the history. I use it for commands that I know I will never recall, or never want to recall, like
rm -rf * :). There's plenty more stuff that I ignore by default (not shown), which keeps the history size down.
Despite the countermeasures above, duplicates still end up in my history (e.g., due to multiple Bash shells open at the same time). I have a counter in my prompt that shows me the current history size
(The relevant code is
\!, a.k.a. history number of this command.) That shows me when it is time to 'clean up' (see next step).
I periodically 'clean up' my Bash history by running the following command:
tac ~/.bash_history | awk '!seen[$0]++' | tac > ~/.bash_history.new && mv ~/.bash_history.new ~/.bash_history
(presuming your Bash history is stored in
~/.bash_history of course). This command removes duplicates while respecting order: commands used last remain last. Only the last (most recent) duplicate is retained.
With this approach, I've been able to keep all of my Bash history within a very modest history size of 600 to 1000 entries.