8ball-media

little pieces from everything

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..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
 #!/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 (david@8ball-media.de)
 # version  0.0.1

 history_path = "/Users/#{ENV['USER']}"
 history_file = ".bash_history"

 history = File.join history_path, history_file

 entries = Array.new
 uniques = Array.new

 begin
   file = File.open(history).each do |line|
     entries.push line
   end
 rescue
   abort "nError! could not find: #{history} ..n "
 end

 uniques = entries.uniq
 removed = (entries.size - uniques.size).to_s

 output = File.new(history, "w+")
 output.write(uniques)
 output.close

 file.close

 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.

~ David

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