ruby遍历文件夹计算md5sum

来源:互联网 发布:python 字符串转json 编辑:程序博客网 时间:2024/06/12 21:44
#!/usr/bin/ruby -w#require 'digest/md5'if ARGV.empty?        puts "usgae: #$0 path"        exit 0enddir_name=ARGV.shiftdef dir_md5sum(path)        md5s=Array.new        if File.directory?(path)                Dir.new(path).each do |file|                        next if file =~ /^\.+$/                        file="#{path}/#{file}"                        if File.directory?(file)                                dir_md5sum(file)                        elsif File.file?(file)                                md5="#{Digest::MD5.hexdigest(File.read(file))} #{file}"                                md5s.push(md5)                        end                end        elsif File.file?(path)                md5="#{Digest::MD5.hexdigest(File.read(path))} #{path}"                md5s.push(md5)        else                puts "Ivalid File type"                exit 2        end        md5s.each do |item|                puts item        endenddir_md5sum(dir_name)