Calculate the MD5 value of a file in Centos

Source: Internet
Author: User

Script Function

The script is written in Perl to calculate the MD5 value of the file.

Script usage

After installing the operating system, the server calculates the MD5 values of all the binary files in the PATH variable, calculates the MD5 values of a single file, and computes a directory (including subdirectories) the MD5 values of all files are recorded in the file, and a new copy is generated later. Then, two files are compared to determine whether these files have been modified.

Script usage (6 usage)

 
 
  1. Script Name-p [> output file name] annotate this function is used to calculate the MD5 value of all files in the PATH variable

  2. Script Name-p-f MD5 file [> output file name]. This function calculates the MD5 value of all files in the PATH variable and keeps up with the generated MD5 file.

  3. Script Name file 1 file 2 file 3... [> output file name] compute this function is used to calculate the MD5 value of a specified file

  4. Script Name directory 1 directory 2 Directory 3... [> output file name] compute this function is used to calculate the MD5 value of all files in a specified directory

  5. Script Name-c MD5 file 1 MD5 file 2 [> output file name]. This function compares the MD5 values of all files in the same directory that are generated twice.

  6. Script Name directory-f MD5 file [> output file name]. This function is similar to the preceding function, but this function is used for direct comparison without generating files.

Notes

Cannot calculate the/proc directory
The/(Root) cannot be calculated because/contains/proc
You cannot calculate the shared directory mounted to Windows on Linux.

Script content

[Version 2]

 
 
  1. #! /Usr/bin/perl

  2. Use Digest: MD5;

  3. Use File: Find;

  4. #2012-12-16 Leo chanyipiaomiao@163.com

  5. # Blog: http://linux5588.blog.51cto.com

  6. # Usage tips

  7. $ Usage = "Usage: scriptname-p | Directory1... | File1... |-c MD5File1 MD5File2 | Directory-f MD5File [> OUTFILE]";

  8. $ Usagecompare = "Usage: scriptname-c MD5File1 MD5File2 [> OUTFILE]";

  9. $ Usagepath = "Usage: scriptname-p |-p-f MD5File [> OUTFILE]";

  10. # Determine whether the command line parameter is null. If it is null, the MD5 value of all the binary files in the PATH is calculated directly.

  11. # If the first parameter is a directory, call getDirectoryAllFileMD5 to calculate the MD5 value of all files in the directory.

  12. # If the first parameter is a file, call getSingleFileMD5 to calculate the MD5 value of all files in the command line parameter

  13. If (@ ARGV ){

  14. My $ arg = $ ARGV [0];

  15. If (-d $ arg ){

  16. If ($ ARGV [1] eq '-F' &-T $ ARGV [2]) {

  17. & CompareWithLastMD5File ($ arg );

  18. } Else {

  19. & GetDirectoryAllFileMD5 (@ ARGV );

  20. }

  21. } Elsif (-f $ arg ){

  22. & GetSingleFileMD5 (@ ARGV );

  23. } Elsif ($ arg eq '-C' & @ ARGV = 3 ){

  24. Die "$ usagecompare \ n" unless (-T $ ARGV [1] &-T $ ARGV [2]);

  25. & CompareWithTwoMD5Files;

  26. } Elsif ($ arg eq '-p '){

  27. If (1 = @ ARGV ){

  28. & GetPathBinFileMD5;

  29. } Elsif ($ ARGV [1] eq '-F' &-T $ ARGV [2]) {

  30. & CompareWithLastPathMD5File;

  31. } Else {

  32. Die "$ usagepath \ n ";

  33. }

  34. } Else {

  35. Die "$ usage \ n ";

  36. }

  37. } Else {

  38. Die "$ usage \ n ";

  39. }

  40. # Obtain the MD5 value of all files (including subdirectories) in the directory

  41. Sub getDirectoryAllFileMD5 {

  42. Find (\ & wantedPrint ,@_);

  43. }

  44. # Obtain the MD5 value of all the binary files in the PATH variable.

  45. Sub getPathBinFileMD5 {

  46. My @ path = split/:/, $ ENV {PATH };

  47. Find (\ & wantedPrint, @ path );

  48. }

  49. # First obtain the MD5 value of the files in all directories of the PATH variable, and then compare it with the previously generated MD5 File

  50. Sub compareWithLastPathMD5File {

  51. My @ path = split/:/, $ ENV {PATH };

  52. & CompareWithLastMD5File (@ path );

  53. }

  54. # Obtain the MD5 value of a single file

  55. Sub getSingleFileMD5 {

  56. Foreach (@_){

  57. If (-R $ _){

  58. Print "$ _", & getMD5 ($ _), "\ n ";

  59. } Else {

  60. Print "Can't read $ _ \ n ";

  61. Next;

  62. }

  63. }

  64. }

  65. # Convert the MD5 values of all files in this directory and compare them with the MD5 files generated in this directory.

  66. # The generated MD5 file is compared with the generated MD5 file. The generated MD5 file is output in different ways at the same time.

  67. # If it is a newly added file, three time values are output, atime mtime ctime

  68. Sub compareWithLastMD5File {

  69. Find (\ & wantedHash, @ _); # after calling the callback function, a hash of % thisMD5Hash is generated.

  70. My $ md5file = $ ARGV [2];

  71. Open LASTMD5FILE, "<", "$ md5file" ordie "Can't read $ md5file: $! \ N ";

  72. My $ lastMD5Filerecords = (@ lastMD5FilerecordsArray = <LASTMD5FILE> );

  73. % LastMD5Hash = map {split} @ lastMD5FilerecordsArray;

  74. Close LASTMD5FILE;

  75. Foreach (keys % thisMD5Hash ){

  76. $ ThisMD5Filerecords ++;

  77. }

  78. & Compare ($ thisMD5Filerecords, $ lastMD5Filerecords );

  79. }

  80. # Compare two generated MD5 files (generated for the same directory) to find different or nonexistent

  81. Sub compareWithTwoMD5Files {

  82. My ($ md5file1, $ md5file2) = ($ ARGV [1], $ ARGV [2]);

  83. Open MD5FILE1, "<", "$ md5file1" ordie "Can't read $ md5file1: $! \ N ";

  84. Open MD5FILE2, "<", "$ md5file2" ordie "Can't read $ md5file2: $! \ N ";

  85. My $ file1record = (@ file1record = <MD5FILE1> );

  86. My $ file2record = (@ file2record = <MD5FILE2> );

  87. Close MD5FILE1;

  88. Close MD5FILE2;

  89. % ThisMD5Hash = map {split} @ file1record;

  90. % LastMD5Hash = map {split} @ file2record;

  91. & Compare ($ file1record, $ file2record );

  92. }

  93. # Compare two MD5 files or generate edge comparison

  94. Sub compare {

  95. My ($ file1record, $ file2record) = ($ _ [0], $ _ [1]);

  96. If ($ file1record >=$ file2record ){

  97. % Hash1 = % thisMD5Hash;

  98. % Hash2 = % lastMD5Hash;

  99. } Else {

  100. % Hash1 = % lastMD5Hash;

  101. % Hash2 = % thisMD5Hash;

  102. }

  103. My $ count = 0;

  104. Foreach (keys % hash1 ){

  105. If (exists $ hash2 {$ _}){

  106. If ($ hash1 {$ _} ne $ hash2 {$ _}){

  107. $ Count ++;

  108. ($ Atime, $ mtime, $ ctime) = & getFileAMCTime ($ _);

  109. Print "Different --> $ _ \ n ";

  110. Print "$ hash2 {$ _} \ n ";

  111. Print "$ hash1 {$ _} Atime: $ atime Mtime: $ mtime Ctime: $ ctime \ n ";

  112. }

  113. } Else {

  114. $ Count ++;

  115. If (-e $ _){

  116. ($ Atime, $ mtime, $ ctime) = & getFileAMCTime ($ _);

  117. Print "Added --> $ _ \ n $ hash1 {$ _} Atime: $ atime Mtime: $ mtime Ctime: $ ctime \ n ";

  118. } Else {

  119. Print "Deleted --> $ _ $ hash1 {$ _} \ n ";

  120. }

  121. }

  122. }

  123. If ($ count = 0 ){

  124. Print "Not Found Different !! \ N ";

  125. }

  126. }

  127. # Traverse the condition and output it after finding it

  128. Sub wantedPrint {

  129. If (-f $ _ &-R $ _){

  130. Print "$ File: Find: name", & getMD5 ($ _), "\ n ";

  131. }

  132. }

  133. # Traverse condition. After finding the condition, a HASH is formed.

  134. Sub wantedHash {

  135. If (-f $ _ &-r $ _){

  136. $ ThisMD5Hash {$ File: Find: name} = & getMD5 ($ _);

  137. }

  138. }

  139. # Calculate the MD5 Value

  140. Sub getMD5 {

  141. My $ file = shift @_;

  142. Open (FH, $ file) ordie "Can't open '$ file': $! \ N ";

  143. Binmode (FH );

  144. My $ filemd5 = Digest: MD5-> new-> addfile (FH)-> hexdigest;

  145. Close FH;

  146. Return $ filemd5;

  147. }

  148. # Obtain the atime, mtime, and ctime of a file

  149. Sub getFileAMCTime {

  150. $ Filename = shift @_;

  151. My ($ atime, $ mtime, $ ctime) = (stat ($ filename) [8, 9, 10];

  152. $ Atime = & getTime ($ atime );

  153. $ Mtime = & getTime ($ mtime );

  154. $ Ctime = & getTime ($ ctime );

  155. # Convert the date and time format to a friendly format

  156. Sub getTime {

  157. My $ time = shift @_;

  158. My ($ sec, $ min, $ hour, $ day, $ mon, $ year) = (localtime $ time) [0 .. 5];

  159. $ Time = sprintf "% 4d-% 02d-% 02d % 2d: % 02d: % 02d", $ year + 1900, $ mon + 1, $ day, $ hour, $ min, $ sec;

  160. Return $ time;

  161. }

  162. Return $ atime, $ mtime, $ ctime;

  163. }


[First version]

 
 
  1. #! /Usr/bin/perl

  2. Use Digest: MD5;

  3. Use File: Find;


  4. #2012-11-24 Leo chanyipiaomiao@163.com

  5. # Blog: http://linux5588.blog.51cto.com


  6. # Usage tips

  7. $ Usage = "Usage: scriptname-p | Directory1... | File1... |-c MD5File1 MD5File2 | Directory-f MD5File [> OUTFILE]";

  8. $ Usagecompare = "Usage: scriptname-c MD5File1 MD5File2 [> OUTFILE]";

  9. $ Usagepath = "Usage: scriptname-p [> OUTFILE]";

  10. # Determine whether the command line parameter is null. If it is null, the MD5 value of all the binary files in the PATH is calculated directly.

  11. # If the first parameter is a directory, call getDirectoryAllFileMD5 to calculate the MD5 value of all files in the directory.

  12. # If the first parameter is a file, call getSingleFileMD5 to calculate the MD5 value of all files in the command line parameter

  13. If (@ ARGV ){

  14. My $ arg = $ ARGV [0];

  15. If (-d $ arg ){

  16. If ($ ARGV [1] eq '-F' &-T $ ARGV [2]) {

  17. & CompareWithLastMD5File ($ arg );

  18. } Else {

  19. & GetDirectoryAllFileMD5 (@ ARGV );

  20. }

  21. } Elsif (-f $ arg ){

  22. & GetSingleFileMD5 (@ ARGV );

  23. } Elsif ($ arg eq '-C' & @ ARGV = 3 ){

  24. Die "$ usagecompare \ n" unless (-T $ ARGV [1] &-T $ ARGV [2]);

  25. & CompareWithTwoMD5Files;

  26. } Elsif ($ arg eq '-p '){

  27. Die "$ usagepath \ n" unless (@ ARGV = 1 );

  28. & GetPathBinFileMD5;

  29. } Else {

  30. Die "$ usage \ n ";

  31. }

  32. } Else {

  33. Die "$ usage \ n ";

  34. }

  35. # Obtain the MD5 value of all files (including subdirectories) in the directory

  36. Sub getDirectoryAllFileMD5 {

  37. Find (\ & wantedPrint ,@_);

  38. }

  39. # Obtain the MD5 value of all the binary files in the PATH variable.

  40. Sub getPathBinFileMD5 {

  41. My @ path = split/:/, $ ENV {PATH };

  42. Find (\ & wantedPrint, @ path );

  43. }

  44. # Obtain the MD5 value of a single file

  45. Sub getSingleFileMD5 {

  46. Foreach (@_){

  47. If (-R $ _){

  48. Print "$ _", & getMD5 ($ _), "\ n ";

  49. } Else {

  50. Print "Can't read $ _ \ n ";

  51. Next;

  52. }

  53. }

  54. }

  55. # Convert the MD5 values of all files in this directory and compare them with the MD5 files generated in this directory.

  56. # The generated MD5 file is compared with the generated MD5 file. The generated MD5 file is output in different ways at the same time.

  57. # If it is a newly added file, three time values are output, atime mtime ctime

  58. Sub compareWithLastMD5File {

  59. Find (\ & wantedHash ,@_);

  60. My $ md5file = $ ARGV [2];

  61. Open MD5FILE, "<", "$ md5file" or die "Can't read $ md5file: $! \ N ";

  62. My $ lastMD5Filerecords = (@ lastMD5FilerecordsArray = <MD5FILE> );

  63. My % lastMD5Hash = map {split} @ lastMD5FilerecordsArray;

  64. Close MD5FILE;

  65. Foreach (keys % thisMD5Hash ){

  66. $ ThisMD5Filerecords ++;

  67. }

  68. If ($ thisMD5Filerecords >=$ lastMD5Filerecords ){

  69. % Hash1 = % thisMD5Hash;

  70. % Hash2 = % lastMD5Hash;

  71. } Else {

  72. % Hash1 = % lastMD5Hash;

  73. % Hash2 = % thisMD5Hash;

  74. }

  75. Foreach (keys % hash1 ){

  76. If (exists $ hash2 {$ _}){

  77. If ($ hash1 {$ _} ne $ hash2 {$ _}){

  78. ($ Atime, $ mtime, $ ctime) = & getFileAMCTime ($ _);

  79. Print "Different: $ _ $ hash1 {$ _} Atime: $ atime Mtime: $ mtime Ctime: $ ctime \ n ";

  80. }

  81. } Else {

  82. If (-e $ _){

  83. ($ Atime, $ mtime, $ ctime) = & getFileAMCTime ($ _);

  84. Print "Added: $ _ $ hash1 {$ _} Atime: $ atime Mtime: $ mtime Ctime: $ ctime \ n ";

  85. } Else {

  86. Print "Deleted: $ _ $ hash1 {$ _} \ n ";

  87. }

  88. }

  89. }

  90. }

  91. # Compare two generated MD5 files (generated for the same directory) to find different or nonexistent

  92. Sub compareWithTwoMD5Files {

  93. My ($ md5file1, $ md5file2) = ($ ARGV [1], $ ARGV [2]);

  94. Open MD5FILE1, "<", "$ md5file1" or die "Can't read $ md5file1: $! \ N ";

  95. Open MD5FILE2, "<", "$ md5file2" or die "Can't read $ md5file2: $! \ N ";

  96. My $ file1record = (@ file1record = <MD5FILE1> );

  97. My $ file2record = (@ file2record = <MD5FILE2> );

  98. Close MD5FILE1;

  99. Close MD5FILE2;

  100. My % hashmap1 = map {split} @ file1record;

  101. My % hashmap2 = map {split} @ file2record;

  102. If ($ file1record >=$ file2record ){

  103. % Hash1 = % hashmap1;

  104. % Hash2 = % hashmap2;

  105. } Else {

  106. % Hash1 = % hashmap2;

  107. % Hash2 = % hashmap1;

  108. }

  109. Foreach (keys % hash1 ){

  110. If (exists $ hash2 {$ _}){

  111. If ($ hash1 {$ _} ne $ hash2 {$ _}){

  112. ($ Atime, $ mtime, $ ctime) = & getFileAMCTime ($ _);

  113. Print "Different: $ _ $ hash1 {$ _} Atime: $ atime Mtime: $ mtime Ctime: $ ctime \ n ";

  114. }

  115. } Else {

  116. If (-e $ _){

  117. ($ Atime, $ mtime, $ ctime) = & getFileAMCTime ($ _);

  118. Print "Added: $ _ $ hash1 {$ _} Atime: $ atime Mtime: $ mtime Ctime: $ ctime \ n ";

  119. } Else {

  120. Print "Deleted: $ _ $ hash1 {$ _} \ n ";

  121. }

  122. }

  123. }

  124. }

  125. # Traverse the condition and output it after finding it

  126. Sub wantedPrint {

  127. If (-f $ _ &-R $ _){

  128. Print "$ File: Find: name", & getMD5 ($ _), "\ n ";

  129. }

  130. }

  131. # Traverse condition. After finding the condition, a HASH is formed.

  132. Sub wantedHash {

  133. If (-f $ _ &-r $ _){

  134. $ ThisMD5Hash {$ File: Find: name} = & getMD5 ($ _);

  135. }

  136. }

  137. # Calculate the MD5 Value

  138. Sub getMD5 {

  139. My $ file = shift @_;

  140. Open (FH, $ file) or die "Can't open '$ file': $! \ N ";

  141. Binmode (FH );

  142. My $ filemd5 = Digest: MD5-> new-> addfile (FH)-> hexdigest;

  143. Close FH;

  144. Return $ filemd5;

  145. }

  146. # Obtain the atime, mtime, and ctime of a file

  147. Sub getFileAMCTime {

  148. $ Filename = shift @_;

  149. My ($ atime, $ mtime, $ ctime) = (stat ($ filename) [8, 9, 10];

  150. $ Atime = & getTime ($ atime );

  151. $ Mtime = & getTime ($ mtime );

  152. $ Ctime = & getTime ($ ctime );

  153. # Convert the date and time format to a friendly format

  154. Sub getTime {

  155. My $ time = shift @_;

  156. My ($ sec, $ min, $ hour, $ day, $ mon, $ year) = (localtime $ time) [0 .. 5];

  157. $ Time = sprintf "% 4d-% 02d-% 02d % 2d: % 02d: % 02d", $ year + 1900, $ mon + 1, $ day, $ hour, $ min, $ sec;

  158. Return $ time;

  159. }

  160. Return $ atime, $ mtime, $ ctime;

  161. }


This article is from the blog of reelcos, please be sure to keep this source http://linux5588.blog.51cto.com/65280/1070915


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.