quick hacks

copy files from a directory to a destination by number offset:

 

#!/usr/bin/perl -w

use strict;
use Getopt::Std;
use File::Copy;

my %opts = ();
getopts('d:n:f:', \%opts);

my $dir = $opts{d};
my $num = $opts{n};
my $frm = $opts{f};
my $to_dir = '/var/log/'.$dir;

die "no directory : [$dir]\n"  unless -d $dir;
die "no files specified : [$num] (must be a number)\n" unless $num =~ m/^\d+$/;
if(defined($frm)) {
  die "no offset specified : [$frm] (must be a number)\n" unless $frm =~ m/^\d+$/;
}

print "processing dir :[$dir]\n";
print "files          :[$num]\n";

print"reading dir ....\n"; 
opendir(DIR, $dir) || die "can't opendir $dir: $!";
my @files = grep { ! /^\./ && -f "$dir/$_" } readdir(DIR);
closedir DIR;
print"dir read ....\n";

my $from = $frm || 0;
print">>DEBUG>>from:[$from]\n";

for (0 .. ($num - 1)) {
  $_ += $from;
  print"copying file:[$dir/$files[$_]] => $to_dir\n";
  copy("$dir/$files[$_]", $to_dir) or die "copy failed : $!\n";
}

__END__

Sii il primo a lasciare un commento. Non essere timido!

Partecipa alla Discussione

Puoi usare i seguenti HTML tag e attributi: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>