Thread Doppelte Dateien erkennen (6 answers)
Opened by Strat at 2005-06-19 01:27

Dubu
 2005-06-20 01:58
#55543 #55543
User since
2003-08-04
2145 Artikel
ModeratorIn + EditorIn

user image
Sowas habe ich mir auch mal geschrieben, um Ordnung auf meiner Festplatte zu halten. Es macht aber keinen tatsaechlichen Dateivergleich, sondern vergleicht nur die MD5-Digests.
Code: (dl )
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/perl
# finddupes - find duplicate files by MD5 digest
#
use strict;
use warnings;
use File::Find;
use Digest::MD5;

my %FilesBySize;

# default: current directory
@ARGV = qw/./ unless @ARGV;

# find all files
find ({wanted => \&add_file, no_chdir => 1}, @ARGV);

find_duplicates();

exit 0;

###########################################

sub add_file {
   return unless -f $_;
   my $size = -s _;
   return unless $size;
   push @{$FilesBySize{$size}}, $File::Find::name;
}

sub find_duplicates {
   for my $s (sort {$a <=> $b} keys %FilesBySize) {
       next unless @{$FilesBySize{$s}} > 1; # more than 1 file?

       my %md5s = ();
       for my $f (@{$FilesBySize{$s}}) {
           my $md5 = calc_md5 ($f);
           push @{$md5s{$md5}}, $f;
       }
       for my $md5 (keys %md5s) {
           if (@{$md5s{$md5}} > 1) {
               print "Identische MD5-Summe (mit $s Bytes): \n";
               print "\t$_\n" for @{$md5s{$md5}};
           }
       }
   }
}

sub calc_md5 {
   my $file = shift;
   open(FILE, $file) or warn "can't open '$file': $!\n";
   binmode(FILE);
   my $md5 = Digest::MD5->new;
   while (<FILE>) {
       $md5->add($_);
   }
   close(FILE);
   return $md5->b64digest;
}

View full thread Doppelte Dateien erkennen