-
Notifications
You must be signed in to change notification settings - Fork 0
/
dupes
executable file
·66 lines (59 loc) · 1.37 KB
/
dupes
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
59
60
61
62
63
64
65
66
#!/usr/bin/perl
my %files=();
my $DUPES_DIR="++dupes";
if(-d $DUPES_DIR) {
warn "Directory already exists: $DUPES_DIR\n";
exit(1)
}
mkdir($DUPES_DIR);
# sort files by date
my @files = sort {(stat $a)[10] <=> (stat $b)[10]} <*>;
my $fileCount = scalar @files;
# for display
my $count=0;
my $lineCount=0;
my $dupeCount=0;
my $sectionCount = 160;
foreach my $file (@files) {
next if -d $file;
next unless -r $file;
my $md5 =`md5 -q "$file"`;
chomp($md5);
if(defined($files{$md5})) {
if(scalar @{ $files{$md5} } == 1) {
$dupeCount++;
}
push @{ $files{$md5} }, $file;
# print "\nln ${file} ${DUPES_DIR}/${file}\n";
# print "pushed $file onto [ @{ $files{$md5} } ] for $md5\n";
} else {
$files{$md5} = [$file];
# print "set files{$md5} = [ @{ $files{$md5} } ] ($file)\n";
}
print "." if $count % 2;
$count++;
if($count > ${sectionCount}) {
$count=0;
$lineCount++;
print " - ", int($fileCount/${sectionCount}) - $lineCount, "\n";
}
}
print("\n\n");
my $moveCount=$dupeCount;
foreach my $md5 (keys %files) {
my @arr = @{ $files{$md5} };
if(scalar @arr > 1) {
print "${key}: @{ $files{$md5} }\n";
foreach $file (@arr) {
link "${file}", "${DUPES_DIR}/${md5}-${file}";
print "${DUPES_DIR}/${md5}-${file}\n";
}
}
if(scalar @arr > 1) {
# skip the first element
shift @arr;
# move the rest to the trash
# system "trash", @arr;
print "rm @arr\n";
}
}