-
Notifications
You must be signed in to change notification settings - Fork 33
/
file_updater.pl
executable file
·76 lines (59 loc) · 1.55 KB
/
file_updater.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
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# License: GPLv3
# Date: 04 November 2012
# https://github.com/trizen
# Update files in a directory, with files from other dirs.
# Example: perl file_updater.pl -o /tmp /root
# /tmp/dir/file.txt is updated with /root/dir/file.txt
# if the file from the /root dir is newer than the file from the /tmp dir.
use 5.010;
use strict;
use warnings;
use File::Copy qw(copy);
use File::Find qw(find);
use Getopt::Std qw(getopts);
use File::Compare qw(compare);
use File::Spec::Functions qw(rel2abs catfile);
my %opts;
getopts('o:', \%opts);
sub usage {
die <<"EOH";
usage: $0 [options] [dirs]
options:
-o <output_dir> : update files in this directory
example: $0 -o /my/path/out /my/path/input
EOH
}
my $output_dir = $opts{o};
if ( not defined $output_dir
or not -d $output_dir
or not @ARGV) {
usage();
}
$output_dir = rel2abs($output_dir);
my @dirs;
foreach my $dir (@ARGV) {
if (not -d -r $dir) {
warn "[!] Invalid dir '$dir': $!\n";
next;
}
push @dirs, rel2abs($dir);
}
sub update_files {
return if $_ eq $output_dir;
return unless -f;
my $filename = substr($_, length($output_dir) + 1);
my $mdays = -M _;
foreach my $dir (@dirs) {
my $file = catfile($dir, $filename);
if (-e $file and -M (_) < $mdays and compare($file, $_) == 1) {
say "Updating: $file -> $_";
copy($file, $_) or do { warn "[!] Copy failed: $!\n" };
}
}
}
find {
no_chdir => 1,
wanted => \&update_files,
} => $output_dir;