Comparing Directories Ruby Style
I recently needed to recursively compare 2 directories by filename. Here’s the solution:
require 'ftools'srcDir = "/home/Music/"destDir = "/home/Music/iTunes/iTunes Music" mHash = Hash.newiHash = Hash.new#Add all files to hashDir[srcDir + '/**/*.mp3'].each do |path| mHash[path.split('/').last] = pathend #Add all files to second hashDir[destDir + "/**/*.mp3"].each do |path| iHash[path.split('/').last] = pathend puts "Source # Files: " + mHash.length.to_sputs "Dest # Files: " + iHash.length.to_s #Compare Hashes and copy Any datamHash.each_key do |key|if iHash[key] == nilputs "Copying " + mHash[key] +" to " + destDirFile.copy(mHash[key],destDir+"/")endend