Skip to content

Instantly share code, notes, and snippets.

@makaroni4
Created May 6, 2015 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makaroni4/ed7d0ae61479a2097ce7 to your computer and use it in GitHub Desktop.
Save makaroni4/ed7d0ae61479a2097ce7 to your computer and use it in GitHub Desktop.
Duplicates finder
require 'digest/md5'
hash = {}
dirname = "files"
Dir.glob("#{dirname}/**/*", File::FNM_DOTMATCH).each do |filename|
next if File.directory?(filename)
# puts 'Checking ' + filename
key = Digest::MD5.hexdigest(IO.read(filename)).to_sym
if hash.has_key? key
# puts "same file #{filename}"
hash[key].push filename
else
hash[key] = [filename]
end
end
hash.each_value do |filename_array|
if filename_array.length > 1
puts "=== Identical Files ===\n"
sorted_by_date = filename_array.sort_by {|filename| File.mtime(filename) }
puts "Original: #{sorted_by_date.first}"
puts "Copies:"
sorted_by_date[1..-1].each do |filename|
puts "\t" + filename
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment