-
Notifications
You must be signed in to change notification settings - Fork 34
/
fdf
executable file
·134 lines (108 loc) · 3.38 KB
/
fdf
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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 01 January 2012
# Edit: 24 August 2024
# https://github.com/trizen
# Find and list duplicate files from one or more paths, with options for
# deleting or replacing duplicate files with symbolic links to the main file.
use 5.005;
use strict;
use warnings;
use File::Find qw(find);
use File::Compare qw(compare);
use File::Basename qw(basename);
use Getopt::Long qw(GetOptions);
my %order_callbacks = (
path => sub { sort @_ },
name => sub {
map { $_->[1] }
sort { $a->[0] cmp $b->[0] }
map { [basename($_), $_] } @_;
},
time => sub {
map { $_->[1] }
sort { $a->[0] <=> $b->[0] }
map { [-M $_, $_] } @_;
},
);
my @dirs = grep { (-d) or (-f) } @ARGV;
die <<"HELP" if !@dirs;
usage: $0 [options] /my/path [...]
Options:
-f, --first : keep only the first duplicated file
-l, --last : keep only the last duplicated file
-s, --symlink : replace duplicate files with symbolic links (with -f or -l)
-o, --order=type : order the results by: path, name or time
-m, --min-size=i : minimum size in bytes (default: 0)
HELP
my $keep_first;
my $keep_last;
my $create_symlinks;
my $order_by = 'time';
my $min_size = 0;
GetOptions(
'f|first!' => \$keep_first,
'l|last!' => \$keep_last,
's|symlink!' => \$create_symlinks,
'o|order|order-by=s' => \$order_by,
'm|min-size=i' => \$min_size,
)
or die("$0: error in command line arguments\n");
if (not exists $order_callbacks{$order_by}) {
local $" = ", ";
die "$0: invalid value `$order_by` for `--order`: valid values are: @{[sort keys %order_callbacks]}\n";
}
sub find_duplicated_files (&@) {
my $callback = shift;
my %files;
find {
no_chdir => 1,
wanted => sub {
lstat;
(-f _) && (not -l _) && ((-s _) >= $min_size) && push @{$files{-s _}}, $_;
}
} => @_;
foreach my $files (values %files) {
next if $#{$files} < 1;
my %dups;
foreach my $i (0 .. $#{$files} - 1) {
for (my $j = $i + 1 ; $j <= $#{$files} ; $j++) {
if (compare($files->[$i], $files->[$j]) == 0) {
push @{$dups{$files->[$i]}}, splice @{$files}, $j--, 1;
}
}
}
while (my ($fparent, $fdups) = each %dups) {
$callback->($order_callbacks{$order_by}($fparent, @{$fdups}));
}
}
return;
}
{
local $, = "\n";
local $\ = "\n";
find_duplicated_files {
my (@files) = @_;
print @files, "-" x 80;
my $main_file = (
$keep_first ? shift(@files)
: $keep_last ? pop(@files)
: return
);
foreach my $file (@files) {
print ":: Removing: `$file`";
unlink($file) or do {
warn "error: can't delete file `$file': $!\n";
next;
};
if ($create_symlinks) {
print ":: Symlinking: `$main_file` <- `$file`";
symlink($main_file, $file) or do {
warn "error: can't create symbolic link for `$file': $!\n";
next;
};
}
}
} @dirs;
}