Keep your ~/.bash_history entries unique

When often using the terminal you don’t want to type certain commands over and over again. Although recent commands are stored in the .bash_history in your home-directory they will be overwritten at some point when a new terminal is opened. So you won’t be able to use Ctrl + R to search for commands you’ve typed longer time ago.
Luckily there is an option to append the terminal commands instead of overwriting them.

$ shopt -s histappend

Add this command to your ~/.bash_profile and you’ll never lose a command anymore.
As a side effect the .bash_history file will grow over time as well as be poluted with a lot of redundant commands.
So i wrote a little ruby script to keep the ~/.bash_profile commands unique. Here we go..

#!/usr/bin/env ruby
# Ruby 1.9.2 
# encoding: utf-8
# This little script keeps the entries in the ~/.bash_history file unique
# Might be useful when working with: $ shopt -s histappend 
# author   david linse (
# version  0.0.1
history_path = "/Users/#{ENV['USER']}"
history_file = ".bash_history"
history = File.join history_path, history_file
entries =
uniques =
  file = do |line|
    entries.push line
  abort "nError! could not find: #{history} ..n "
uniques = entries.uniq
removed = (entries.size - uniques.size).to_s
output =, "w+")
msg = ''
msg << "nBash History Uniquifiern
Removed #{removed} entries from #{entries.size}, 
now #{uniques.size} in #{history}n "
puts msg

You can use and/or modify it to your own needs.
Maybe there is someone of you who’s running Ruby on an Windowz machine and could implement
an appropriate modification and post it back.. ?

That’s it, more simple ruby stuff to come..
Thanks for reading and feel free to leave a comment.


Update: I added some error handling for the case the history does not exist.

This entry was posted in experiments, stuff and tagged , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">