Last active
August 29, 2015 14:10
-
-
Save tadejm/b285ae126aa41e3bb066 to your computer and use it in GitHub Desktop.
Finds duplicate files and moves them to Trash.
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
#!/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