-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_newest_dupes.pl
executable file
·224 lines (173 loc) · 4.94 KB
/
remove_newest_dupes.pl
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/usr/bin/perl -w
use strict;
# Specify external "cksum" tool to use to identify duplicate/unique files:
my $CKSUM="md5sum";
use constant RM_NEWEST => 1;
use constant RM_OLDEST => 2;
my $RM=RM_NEWEST;
my $VERBOSE=0;
my $DO_REMOVALS=0;
my %XXXEST_FILE_BY_CKSUM=();
my %SEEN=();
my @ITEM_LIST=();
my $FILES=0;
my $UNIQUE_FILES=0;
my $DELETED_FILES=0;
my $KEPT_FILES=0;
################################################################################
# SUBS:
sub usage {
## START USAGE: ########################################
print <<EOUSAGE;
Usage: $0
$0 [-h|-help] [-do] [-old] [-v] [<FILES>]
-h|help: Show this message
-do: Doit - actually delete the selected files
-old: Removed oldest dupes
-v: Increase verbosity
This script detects duplicate files and removes the newest duplicates
(or oldest if the -old option is specified)
This can be useful for example if a cron script creates some daily status files
e.g. the output of dpkg -l
So we keep only the files as they change and not any intermediate duplicates.
e.g.
To remove newest duplicated files in current dir:
$0
To remove oldest duplicated files in current dir:
$0 -old
To remove oldest duplicated files from provided list:
$0 -old FILE1 FILE1_NEWER FILE1_VERYOLD FILE2 FILE2_OLD
Would remove files FILE1 FILE1_VERYOLD FILE2_OLD";
Keeping oldest copies of FILE1, FILE2: FILE1_NEWER FILE2
EOUSAGE
## END USAGE: ##########################################
}
sub get_mtime {
my $file = shift;
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat($file);
return $mtime;
}
################################################################################
# CMD-LINE ARGS:
while ($_ = shift(@ARGV)) {
if (/^-(h|help)$/) {
usage();
exit(0);
}
if (/^-(v+)$/) {
$VERBOSE += length($1);
next;
}
if (/^-do/) {
$DO_REMOVALS=1;
next;
}
if (/^-old$/) {
$RM=RM_OLDEST;
next;
}
if (-f $_) {
push(@ITEM_LIST, $_);
next;
}
usage();
die "Unknown option: '$_'";
}
if (! @ITEM_LIST) {
opendir(DIR, ".");
@ITEM_LIST=grep(!/^\.+$/, grep(-f, readdir(DIR)));
closedir(DIR)
}
################################################################################
# MAIN:
for $_ ( @ITEM_LIST ) {
$FILES++;
if ($SEEN{$_}) {
if ($VERBOSE > 1) {
print "ALREADY SEEN: $_\n";
}
next;
}
$SEEN{$_}=1;
$UNIQUE_FILES++;
my $cksum = `$CKSUM < $_`;
chomp($cksum);
if ( ! defined( $XXXEST_FILE_BY_CKSUM{$cksum} ) ) {
$XXXEST_FILE_BY_CKSUM{$cksum} = $_;
if ($VERBOSE > 1) {
print "NEW<$cksum>: $_ [" , get_mtime($_) , "]\n";
}
next;
}
if ($VERBOSE > 1) {
print "OLD<$cksum>: $_ [" , get_mtime($_) , "]\n";
}
my $prev_file = $XXXEST_FILE_BY_CKSUM{$cksum};
my $prev_mtime=get_mtime( $prev_file );
my $mtime=get_mtime( $_ );
if ($mtime == $prev_mtime) {
warn "NOT HANDLED: ($mtime == $prev_mtime)";
next;
}
my $NEWER = $mtime > $prev_mtime;
my $OLDER = $mtime < $prev_mtime;
#my $REMOVE_OLDEST = ($RM == RM_OLDEST) && $NEWER;
#my $REMOVE_NEWEST = ($RM == RM_NEWEST) && $OLDER;
if (($RM == RM_OLDEST) && $OLDER) {
print "$_ is OLDER than $prev_file => rm $_\n";
$XXXEST_FILE_BY_CKSUM{$cksum} = $prev_file;
if ($DO_REMOVALS) {
print "unlink($_);\n";
$DELETED_FILES++;
unlink($_);
}
next;
}
if (($RM == RM_OLDEST) && $NEWER) {
print "$_ is NEWER than $prev_file => rm $prev_file\n";
$XXXEST_FILE_BY_CKSUM{$cksum} = $_;
if ($DO_REMOVALS) {
print "unlink($prev_file);\n";
$DELETED_FILES++;
unlink($prev_file);
}
next;
}
if (($RM == RM_NEWEST) && $NEWER) {
print "$_ is NEWER than $prev_file => rm $_\n";
$XXXEST_FILE_BY_CKSUM{$cksum} = $prev_file;
if ($DO_REMOVALS) {
print "unlink($_);\n";
$DELETED_FILES++;
unlink($_);
}
next;
}
if (($RM == RM_NEWEST) && $OLDER) {
print "$_ is OLDER than $prev_file => rm $prev_file\n";
$XXXEST_FILE_BY_CKSUM{$cksum} = $_;
if ($DO_REMOVALS) {
print "unlink($prev_file);\n";
$DELETED_FILES++;
unlink($prev_file);
}
next;
}
}
END {
if ($VERBOSE) {
$KEPT_FILES=keys %XXXEST_FILE_BY_CKSUM;
print "Removed $DELETED_FILES of $FILES files\n";
if ( $UNIQUE_FILES != $FILES) {
print "Saw $UNIQUE_FILES unique files of $FILES\n";
}
}
if ($VERBOSE > 1) {
print "Kept $KEPT_FILES of $FILES files:\n";
for my $cksum ( keys %XXXEST_FILE_BY_CKSUM ) {
print " $XXXEST_FILE_BY_CKSUM{$cksum}\n";
}
}
}