Skip to content

Instantly share code, notes, and snippets.

@tadejm
Last active August 29, 2015 14:10
Show Gist options
  • Save tadejm/b285ae126aa41e3bb066 to your computer and use it in GitHub Desktop.
Save tadejm/b285ae126aa41e3bb066 to your computer and use it in GitHub Desktop.
Finds duplicate files and moves them to Trash.
#!/usr/bin/ruby
require 'digest'
require 'set'
require 'pathname'
require 'fileutils'
uniques = Set.new
user = `whoami`.sub("\n","")
trash = Pathname.new("/Users/#{user}/.Trash/")
errors = []
path = ARGV[0]
unless path
puts "USAGE: ruby #{__FILE__} PATH"
exit 1
end
directory = Pathname.new(path)
if !directory.directory?
puts "ERROR: #{path} is not a directory"
exit 1
end
Dir.foreach(directory) do |file|
filename = Pathname.new(directory + file)
next if filename.directory?
md5 = Digest::MD5.file(filename).to_s
FileUtils.mv(filename, trash) if uniques.include?(md5)
uniques << md5
end
puts errors
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment