-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfile-append-sha1
executable file
·49 lines (43 loc) · 1.04 KB
/
file-append-sha1
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
#!/usr/bin/env perl
use v5.18;
use strict;
use warnings;
use File::Copy qw(move);
use File::Basename qw(dirname fileparse);
use File::Path qw(make_path);
use File::Spec::Functions qw(catfile catdir);
use File::Next;
use Getopt::Long;
use Digest::SHA1;
sub sha1_digest {
my ($file) = @_;
open my $fh, "<", $file;
my $sha1 = Digest::SHA1->new;
$sha1->addfile($fh);
return $sha1->hexdigest;
}
sub main {
my ($opts, $args) = @_;
my @input = grep { -f || -d } @{ $opts->{i} };
my $iter = File::Next::files(@input);
my %plan;
while (defined( my $file = $iter->() )) {
next if $file =~ / \.DS_Store \z/x;
my ($filename, $dir, $ext) = fileparse($file, qr/\.[^.]*/);
my $sha1 = sha1_digest($file);
my $newfile = catfile($dir, "${filename}-${sha1}${ext}");
unless (-f $newfile) {
say "mv $file $newfile";
move($file, $newfile);
}
}
}
my %opts;
GetOptions(
\%opts,
'i=s@',
'd=s',
'n',
'ignore-existing'
);
main(\%opts, [@ARGV]);