Skip to content

Instantly share code, notes, and snippets.

@ldunn
Created November 18, 2009 01:18
Show Gist options
  • Save ldunn/237465 to your computer and use it in GitHub Desktop.
Save ldunn/237465 to your computer and use it in GitHub Desktop.
#!/usr/bin/ruby
require 'digest/md5'
require 'find'
@hashfun = Digest::MD5.new
@files = Hash.new()
@filenames = Hash.new()
@hashes = Array.[]
def hashsum(filename)
file = File.new(filename)
contents = file.read()
return @hashfun.hexdigest(contents)
end
def checkdupe(filename)
filehash = @files[filename]
for hash in @hashes
if filehash == hash
puts filename + " and " + @filenames[hash] + " are duplicate."
end
end
end
puts "Enter dir name to search:"
dir = gets.strip()
dirs = [dir]
for dir in dirs
Find.find(dir) do |path|
if FileTest.directory?(path)
else
hash = hashsum(path)
@files[path] = hash
@filenames[hash] = path
checkdupe(path)
@hashes << hash
# puts @files[path]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment