linux 生成文件的md5,Linux下计算文件的MD5值

#!/usr/bin/perl

use Digest::MD5;

use File::Find;

# 2012-11-24 22:41 Leo chanyipiaomiao@163.com

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

#用法提示

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

$usagecompare = "Usage: scriptname -c MD5File1 MD5File2 [ > OUTFILE]";

$usagepath = "Usage: scriptname -p [ > OUTFILE]";

#判断命令行参数是否为空,为空 则直接计算PATH路径里面所有的二进制文件的MD5值,不为空

#如果第一个参数是目录,那么调用getDirectoryAllFileMD5 计算目录里面所有文件的MD5值

#如果第一个参数是文件,那么调用getSingleFileMD5 计算命令行参数里面所有文件的MD5值

if (@ARGV) {

my $arg = $ARGV[0];

if (-d $arg ) {

if ($ARGV[1] eq '-f'&& -T $ARGV[2]){

&compareWithLastMD5File($arg);

} else{

&getDirectoryAllFileMD5(@ARGV);

}

} elsif ( -f $arg ) {

&getSingleFileMD5(@ARGV);

} elsif ($arg eq '-c'&& @ARGV == 3 ) {

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

&compareWithTwoMD5Files;

} elsif ($arg eq '-p') {

die "$usagepath\n"unless (@ARGV == 1);

&getPathBinFileMD5;

} else{

die "$usage\n";

}

} else{

die "$usage\n";

}

#得到目录下所有文件(包括子目录)的MD5值

sub getDirectoryAllFileMD5 {

find(\&wantedPrint,@_);

}

#得到PATH变量里面所有的二进制文件的MD5值

sub getPathBinFileMD5 {

my @path = split /:/,$ENV{PATH};

find(\&wantedPrint,@path);

}

#得到单个文件的MD5值

sub getSingleFileMD5 {

foreach (@_) {

if (-R $_) {

print "$_ ",&getMD5($_),"\n";

} else{

print "Can't read $_\n";

next;

}

}

}

#先生成该目录下所有文件的MD5值,然后跟上一次该目录的生成的MD5文件对比

#本次生成的MD5跟上一次生成的MD5文件比对,不一致的输出出来,同时将3个时间输出出来

#如果是新添加的文件则输出出来其3个时间值,atime mtime ctime

sub compareWithLastMD5File {

find(\&wantedHash,@_);

my $md5file = $ARGV[2];

openMD5FILE,"

my $lastMD5Filerecords = (@lastMD5FilerecordsArray = );

my %lastMD5Hash = map { split } @lastMD5FilerecordsArray;

closeMD5FILE;

foreach (keys %thisMD5Hash) {

$thisMD5Filerecords++;

}

if ($thisMD5Filerecords >= $lastMD5Filerecords) {

%hash1 = %thisMD5Hash;

%hash2 = %lastMD5Hash;

} else{

%hash1 = %lastMD5Hash;

%hash2 = %thisMD5Hash;

}

foreach (keys %hash1) {

if (exists $hash2{$_}) {

if ( $hash1{$_} ne $hash2{$_} ) {

($atime,$mtime,$ctime) = &getFileAMCTime($_);

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

}

} else{

if (-e $_) {

($atime,$mtime,$ctime) = &getFileAMCTime($_);

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

} else{

print "Deleted:$_ $hash1{$_}\n";

}

}

}

}

#比较2个生成的MD5文件(对同一个目录生成的),找出不同的或者不存在的

sub compareWithTwoMD5Files {

my ($md5file1,$md5file2) = ($ARGV[1],$ARGV[2]);

openMD5FILE1,"

openMD5FILE2,"

my $file1record = (@file1record = );

my $file2record = (@file2record = );

closeMD5FILE1;

closeMD5FILE2;

my %hashmap1 = map { split } @file1record;

my %hashmap2 = map { split } @file2record;

if ($file1record >= $file2record) {

%hash1 = %hashmap1;

%hash2 = %hashmap2;

} else{

%hash1 = %hashmap2;

%hash2 = %hashmap1;

}

foreach (keys %hash1) {

if (exists $hash2{$_}) {

if ( $hash1{$_} ne $hash2{$_} ) {

($atime,$mtime,$ctime) = &getFileAMCTime($_);

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

}

} else{

if (-e $_) {

($atime,$mtime,$ctime) = &getFileAMCTime($_);

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

} else{

print "Deleted:$_ $hash1{$_}\n";

}

}

}

}

#遍历条件,找到之后输出

sub wantedPrint {

if (-f $_ && -R $_) {

print "$File::Find::name ",&getMD5($_),"\n";

}

}

#遍历条件,找到之后形成一个HASH

sub wantedHash {

if (-f $_ && -r $_) {

$thisMD5Hash{$File::Find::name} = &getMD5($_);

}

}

#计算MD5值

sub getMD5 {

my $file = shift @_;

open(FH, $file)ordie"Can't open '$file': $!\n";

binmode(FH);

my $filemd5 = Digest::MD5->new->addfile(FH)->hexdigest;

closeFH;

return$filemd5;

}

#获取文件的atime,mtime,ctime

sub getFileAMCTime {

$filename = shift @_;

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

$atime = &getTime($atime);

$mtime = &getTime($mtime);

$ctime = &getTime($ctime);

#将日期时间格式转换为比较友好的格式

sub getTime {

my $time= shift @_;

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

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

return$time;

}

return$atime,$mtime,$ctime;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值