#!/usr/bin/ruby -w
#
require 'digest/md5'
if ARGV.empty?
puts "usgae: #$0 path"
exit 0
end
dir_name=ARGV.shift
def 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
end
end
dir_md5sum(dir_name)
ruby遍历文件夹计算md5sum
最新推荐文章于 2023-08-14 10:15:26 发布
本文介绍了一段使用Ruby脚本实现目录及其子目录内所有文件的MD5校验码生成方法,包括如何处理目录和文件,以及如何输出校验码。
1242

被折叠的 条评论
为什么被折叠?



