From: Robert Klemme Date: 2011-03-08T18:49:11+09:00 Subject: Re: Checking if two files are the same On Tue, Mar 8, 2011 at 9:48 AM, Brian Candler wrote: > New C. wrote in post #985925: >> I have a got a few folders which may have same files under different >> names. >> Is there any way I can find which these files are using ruby ? > > Here is a little ruby script I use for finding and/or deleting duplicate > image and video files downloaded from my camera - it will work for any > sort of file. > > #!/usr/bin/ruby -w > require 'digest/sha1' > if ARGV[0] == "-d" >  do_delete = true >  ARGV.shift > end > > seen = {} > dirs = ARGV.empty? ? ["#{ENV["HOME"]}/Pictures"] : ARGV > > dirs.each do |dir| >  Dir["#{dir}/**/*"].sort.each do |fn| >    next if File.directory?(fn) >    hash = Digest::SHA1.file(fn).hexdigest >    if seen[hash] >      puts "#{fn} is dupe of #{seen[hash]}" >      if do_delete >        File.delete(fn) >        puts "DELETED" >      end >    else >      seen[hash] = fn >    end >  end > end For this idiom Hash#fetch can be used nicely: irb(main):008:0> h={}; 10.times {|i| puts i h.fetch(i % 3) {|x| printf "first %p\n", i; h[x]=true; nil} and printf "duplicate %p\n", i} 0 first 0 1 first 1 2 first 2 3 duplicate 3 4 duplicate 4 5 duplicate 5 6 duplicate 6 7 duplicate 7 8 duplicate 8 9 duplicate 9 => 10 irb(main):009:0> ... for arbitrary values of "nice". ;-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/