Ruby traverses the folder and calculates the md5sum of the file

Time:2022-4-30

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

#!/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)

 

Recommended Today

IOS memory partition

#Stack area It is a continuous data structure expanded from high address to low address, which is used to store the values of local variables and function parameters #Heap area Programmers need to manage and release the data structure domain expanded by low address like high address and discontinuous memory area #Global area Global variables […]