Created
March 16, 2014 08:59
-
-
Save mwermuth/9580400 to your computer and use it in GitHub Desktop.
Find duplicate Files and delete all except one
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'digest/md5' | |
hash = {} | |
Dir.glob("**/*").each do |filename| | |
next if File.directory?(filename) | |
key = Digest::MD5.hexdigest(IO.read(filename)).to_sym | |
if hash.has_key? key | |
hash[key].push filename | |
else | |
hash[key] = [filename] | |
end | |
end | |
hash.each_value do |filename_array| | |
if filename_array.length > 1 | |
filename_array.each_with_index do |filename, i| | |
if i > 0 | |
puts "=== Deleted Identical File ===\n" | |
File.delete(filename) | |
else | |
puts "=== Original ===\n" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment