Skip to content

Instantly share code, notes, and snippets.

@danopia
Forked from ldunn/dupes.rb
Created November 18, 2009 01:20
Show Gist options
  • Save danopia/237466 to your computer and use it in GitHub Desktop.
Save danopia/237466 to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
require 'digest/md5'
require 'find'
@hashfun = Digest::MD5.new
@digests = {}
def digest filename
@hashfun.hexdigest File.read(filename)
end
def checkdup base, filename
hash = digest filename
filename.sub! base, '' if filename.index(base) == 0
if @digests.has_key? hash
old = @digests[hash].first
old += " (plus #{@digests[hash].size - 1} others)" if @digests[hash].size > 1
puts "#{filename} and #{old} are duplicates."
@digests[hash] << filename
else
@digests[hash] = [filename]
end
end
bases = ARGV
if bases.empty?
print "Enter dir name to search: "
bases << gets.chomp
end
one_base = bases.size == 1
bases.each do |base|
Find.find base do |path|
checkdup( (one_base ? base : ''), path) unless File.directory? path
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment