Skip to content

Instantly share code, notes, and snippets.

@solarmicrobe
Forked from ma11hew28/find-duplicate-files.rb
Created December 14, 2015 20:03
Show Gist options
  • Save solarmicrobe/2b8aad4d1771c6c8089a to your computer and use it in GitHub Desktop.
Save solarmicrobe/2b8aad4d1771c6c8089a to your computer and use it in GitHub Desktop.
Ruby script that finds identical (md5) files in all subdirectories (recursive)
require 'digest/md5'
hash = {}
Dir.glob("**/*", 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"
filename_array.each { |filename| puts ' '+filename }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment