如何编写Perl脚本来计算目录中每个文件的MD5总和? [英] How could I write a Perl script to calculate the MD5 sum of every file in a directory?

查看:191
本文介绍了如何编写Perl脚本来计算目录中每个文件的MD5总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以编写Perl脚本来计算目录中每个文件的MD5sum?

Is there any way to write a Perl script to calculate the MD5sum of every file in a directory?

如果是这样,我该怎么办?

If so, how could I do this?

推荐答案

有很多方法可以执行此操作,但是归结为您需要执行两个操作.您首先需要找到要运行检查的文件列表,然后需要在每个文件上运行md5sum检查.有很多方法可以做到这一点,但是以下应该可以满足您的需求.

Well there are many ways to do this but it comes down to two operations you need to preform. You will first need to locate a list of the files you would like to run the check and then you will need to run the md5sum check on each of those files. There is a ton of ways to do this but the following should work for your needs.

#!/usr/bin/perl

use strict;
use warnings;
use Digest::MD5 qw(md5_hex);

my $dirname = "/home/mgreen/testing/";
opendir( DIR, $dirname );
my @files = sort ( grep { !/^\.|\.\.}$/ } readdir(DIR) );
closedir(DIR);

print "@files\n";

foreach my $file (@files) {
    if ( -d $file || !-r $file ) { next; }
    open( my $FILE, $file );
    binmode($FILE);
    print Digest::MD5->new->addfile($FILE)->hexdigest, " $file\n";
    close($FILE);
}

以上内容将获取目录中每个文件的md5sum,并跳过所有子目录,并将其打印到STDOUT.然后,通过 Digest :: MD5 模块完成MD5校验和部分.认为您正在寻找.

The above will grab the md5sum for each file in the directory and skip any sub-directories and print it to STDOUT. The MD5 checksum part is then done by the Digest::MD5 module which is ultimately what I think you are looking for.

我很喜欢您的问题,因为它是开放式的,提供了许多可能的解决方案,例如所有如何在perl中做到这一点?".问题,所以我相信您会获得很多可能的解决方案,并且最有可能在以后回家时更新我的​​信息.

I do like your question though as it is open-ended with alot of possible solutions like all "How do I do this in perl?" questions so I am sure you will get alot of possible solutions and I will most likely update mine when I get home later.

这篇关于如何编写Perl脚本来计算目录中每个文件的MD5总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆