I have a file that looks like this:
ftp://url1/files1.tar.gz dir1
ftp://url2/files2.txt dir2
.... many more...
What I want to do are these steps:
But how come this approach of mine doesn't work
while(<>) {
chomp;
my ($url,$dir) = split(/\t/,$_);
system("mkdir $dir");
system("cd $dir");
system("wget $url"); # This doesn't get executed
}
What's the right way to do it?
Use native Perl solutions where possible:
cd can be done with chdirmkdir can be done with mkdirmkdir -p (don't die if dir exists, recursive creation) can be done with File::Path which comes with Perlwget can be done with LWP::SimpleHow I would implement this:
use File::Spec::Functions qw(catfile); # adds a '/' between things (or '\' on Windows)
use LWP::Simple qw(mirror);
use File::Path qw(mkpath);
use File::Basename;
use URI;
while (<>) {
chomp;
my ($url, $dir) = split /\t/;
mkpath($dir);
# Use the 'filename' of the $url to save
my $file = basename(URI->new($url)->path);
mirror($url, catfile($dir, $file));
}
If you do this, you get:
die)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With