Last active
January 25, 2022 18:48
-
-
Save ma11hew28/571405 to your computer and use it in GitHub Desktop.
Ruby script that finds identical (md5) files in all subdirectories (recursive)
This file contains 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
# This Ruby script (regardless of where it's located on the file system) recur- | |
# sively lists all duplicate files in the direcotry in which it's executed. | |
require 'digest/md5' | |
hash = {} | |
Dir.glob('**/*', File::FNM_DOTMATCH).each do |f| | |
next if File.directory?(f) | |
key = Digest::MD5.hexdigest(IO.read(f)).to_sym | |
if hash.has_key?(key) then hash[key].push(f) else hash[key] = [f] end | |
end | |
hash.each_value do |a| | |
next if a.length == 1 | |
puts '=== Identical Files ===' | |
a.each { |f| puts "\t" + f } | |
end |
As a one-liner, using a few new things:
files = Pathname(".").glob("**/*").reject(&:directory?); d = files.map.with_index {|f, i| puts "%i / %i" % [i, files.size] if i % 1000 == 12; [f, Digest::MD5.file(f)] }.group_by(&:last).select {|a,v| v.size > 1 }
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://gist.github.com/mailethiesen/146e4d9e9f54dc0bfbc02d104960dbf2 ME TOO! Thank you!