Skip to content

Instantly share code, notes, and snippets.

@jaspervdj
Created June 29, 2012 09:18
Show Gist options
  • Save jaspervdj/3016875 to your computer and use it in GitHub Desktop.
Save jaspervdj/3016875 to your computer and use it in GitHub Desktop.
Remove all file duplicates in a directory, based on file content
#!/usr/bin/ruby
require 'digest'
require 'fileutils'
entries = Dir.entries('.').select { |x| File.file? x }
hashes = {}
entries.each do |entry|
hash = Digest::MD5.file(entry).to_s
if hashes[hash]
puts "Removing #{entry} (duplicate of #{hashes[hash]})"
FileUtils.rm entry
else
hashes[hash] = entry
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment