Checksum for a whole folder in Ruby

Generates a checksum for a given folder without considering updated_at/created_at/permissions, just the content.

def self.checksum(dir)
  files = Dir["#{dir}/**/*"].reject{|f| File.directory?(f)}
  content = files.map{|f| File.read(f)}.join
  require 'md5'
  MD5.md5(content).to_s
end

Simpler but with modification/user-rights etc:

Unix

tar cf - /dir | md5sum

Mac

tar cf - /dir | md5

2 thoughts on “Checksum for a whole folder in Ruby

  1. but it is not recursive, only sums the files at the ‘folder/’ level,
    `md5sum folder/**/* | md5sum` + `md5sum folder/* | md5sum` would do this trick somewhat

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s