Created
June 8, 2016 14:28
-
-
Save milothiesen/bdea4b4b88ba4b11c13f919d8bf0e480 to your computer and use it in GitHub Desktop.
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/env ruby | |
#from http://www.psteiner.com/2007/06/ruby-script-find-duplicate-files.html | |
require 'find' | |
library_path = ARGV[0] | |
dir = Dir.glob(library_path + "/**/*").select{ |x| File.file? x } | |
files = {} | |
found = {} | |
# read root directory from command line | |
dir.each do |arg| | |
Find.find(arg) do |f| | |
if File.file?(f) | |
# accumulate the file names | |
files[f] = File.basename(f) | |
end | |
end | |
end | |
# count up the number of each file name | |
files.each_value do |base| | |
# Ruby doesn't allow this Perl idiom: found[base]++ | |
found[base] = 0 if !found[base] | |
found[base] += 1 | |
end | |
# print the path of each file found more than once, | |
# prepended with rm command commented out | |
found.each do |name,count| | |
if count > 1 | |
files.each do |path,filename| | |
if name == filename | |
puts "# rm #{path}" | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment