Skip to content

Commit

Permalink
Check history file size before proceeding
Browse files Browse the repository at this point in the history
  • Loading branch information
lojic committed Feb 24, 2012
1 parent 910ca08 commit 4c1f584
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions dedup_bash_history.rb
Expand Up @@ -30,9 +30,14 @@
# echo "hello # echo "hello


module DeDuper module DeDuper
NUM_BACKUP_FILES = 20
HISTORY_FILE = File.expand_path('~/.bash_history')
BACKUP_FILE = File.expand_path('~/.bash_history_backup') BACKUP_FILE = File.expand_path('~/.bash_history_backup')
HISTORY_FILE = File.expand_path('~/.bash_history')

# Occasionally, the .bash_history file can be truncated to the
# default size of 500 lines. If this occurs, I don't want to do
# anything until the file has been repaired.
MIN_HIST_LINES = 4000
NUM_BACKUP_FILES = 20
TEMP_FILE = File.expand_path('~/.bash_history_temp') TEMP_FILE = File.expand_path('~/.bash_history_temp')


module_function module_function
Expand All @@ -41,13 +46,22 @@ def rotate_backup_files n
return if n < 1 return if n < 1


if File.exist?(current_file = "#{BACKUP_FILE}#{n}") if File.exist?(current_file = "#{BACKUP_FILE}#{n}")
`cp #{current_file} #{BACKUP_FILE}#{n+1}` `mv #{current_file} #{BACKUP_FILE}#{n+1}`
end end


rotate_backup_files(n-1) rotate_backup_files(n-1)
end end


def history_file_size path
`wc #{HISTORY_FILE}`.strip.split[0].to_i rescue 0
end

def run def run
if history_file_size(HISTORY_FILE) < MIN_HIST_LINES
puts "History file is too small - exiting"
exit 0
end

rotate_backup_files(NUM_BACKUP_FILES) rotate_backup_files(NUM_BACKUP_FILES)
`cp #{HISTORY_FILE} #{BACKUP_FILE}1` `cp #{HISTORY_FILE} #{BACKUP_FILE}1`


Expand Down

0 comments on commit 4c1f584

Please sign in to comment.