-
Notifications
You must be signed in to change notification settings - Fork 1
/
dups
executable file
·53 lines (43 loc) · 1013 Bytes
/
dups
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
#! /usr/bin/perl
use warnings;
use strict;
use feature qw{ say };
use Syntax::Construct '//';
use File::Find;
use Digest::MD5 qw{ md5_hex };
@ARGV or @ARGV = '.';
my %size;
for my $path (@ARGV) {
if (-d $path) {
find({ wanted => \&search, no_chdir => 1 }, $path);
} else {
search($path);
}
}
sub search {
my $file = shift // $File::Find::name;
if (-f $file) {
push @{ $size{-s $file} }, $file unless -l $file;
}
}
for my $s (keys %size) {
next if 1 == ( my @files = @{ $size{$s} } );
my %md5;
for my $f (@files) {
open my $FH, '<', $f or die $!;
my $digest = md5_hex( do { local $/; <$FH> } );
push @{ $md5{$digest} }, $f;
}
for my $digest (keys %md5) {
next if 1 == ( my @same = @{ $md5{$digest} } );
say "-- $s $digest --";
say for @same;
say q();
}
}
=head1 NAME
dups - search for files with duplicate contents
=head1 SYNOPSIS
dups
dups file1 file2 /path/to/dir1
=cut