#!/usr/bin/env ruby # dupremove.rb # Jason Jones # 1/2/2006 # Usage: dupremove.rb DIRECTORY (optional) # Finds all duplicate items starting at # DIRECTORY. If DIRECTORY is not given, # the script's directory is used. require 'find' def removeDupes(dir) Find.find(dir) {|x| if File.ftype(x) == "directory" if File.basename(x)[0]==?. Find.prune else next end else if File.readable?(x) hash = `md5sum "#{x}"` if @h.key?(hash) @a << x else @h[hash] = x end end end } end def report() puts @a.length.to_s + " duplicate items found" if @a.length > 0 @a.each{|x| puts x} puts "Remove (y/n)?" option = STDIN.gets().chomp! if option == "y" @a.each{|x| File.delete(x)} end end end @h = Hash.new @a = Array.new usedir = if(ARGV[0]) then ARGV[0] else Dir.getwd end removeDupes(usedir) report()