The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
Google UNIX.COM


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how can i pick the latest log file as per below mail2sant Shell Programming and Scripting 1 04-05-2008 05:29 AM
Read the latest file wereyou UNIX for Dummies Questions & Answers 3 02-09-2008 08:12 PM
get latest file via ftp command alx Post Here to Contact Site Administrators and Moderators 2 01-26-2006 07:44 AM
get latest file inquirer Shell Programming and Scripting 3 07-06-2003 09:44 PM
How to Grab the latest file n9ninchd UNIX for Dummies Questions & Answers 1 05-10-2001 01:31 PM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 02-08-2008
Registered User
 

Join Date: Feb 2008
Posts: 2
Getting latest file from ftp

Hi,

i have multile JAMA01.DAT.* files in my ftp.

how can i get the latest file in from the ftp by executing the script ?

Regards,

Arun S
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 02-08-2008
Registered User
 

Join Date: Jan 2008
Posts: 10
Execute the below command

ls -ltr
Reply With Quote
  #3 (permalink)  
Old 02-08-2008
Registered User
 

Join Date: Feb 2008
Posts: 2
hi Ganesh,

This wil display all the files listed in the ftp.

but how can i get the latest file ???


Regards,
Arun S
Reply With Quote
  #4 (permalink)  
Old 02-08-2008
HPAVC's Avatar
Registered User
 

Join Date: Feb 2008
Posts: 105
Quote:
Originally Posted by arunavlp View Post
hi Ganesh,
This wil display all the files listed in the ftp.
but how can i get the latest file ???
Regards,
Arun S
You could get them all and sort all that out locally or something like this quick hack (very dependent on output of LIST command)

Code:
#!/usr/bin/perl                                                                                                              

use strict;
#use Data::Dumper;                                                                                                           
use Net::FTP;
use Date::Parse;

my $site  = 'localhost';
my $user  = 'anonymous';
my $pass  = 'nobody@localhost.com';
my $path  = '/';
my $regex = '/\s+JAMA01\.DAT.*$/';
my $debug = 0;

my $ftp = Net::FTP->new($site, Debug => $debug)
    or die "Cannot connect to some.host.name: $@";

$ftp->login($user, $pass)
    or die "Cannot login ", $ftp->message;

$ftp->cwd($path)
    or die "Cannot change working directory ", $ftp->message;

my (@items) = $ftp->dir()
    or die "Cannot list directory ", $ftp->message;

my %files;
my $new_file;
my $new_ts=0;

for my $item (@items)
{
    next unless($item =~ m/^-/); # only real files                                                                           
    next unless(grep($regex, $item));

    my $ts;
    my $file;

    $item =~ m/\s+(\w+\s+\d+\s+\d+:\d+)\s+(.*)$/;
    $ts   = $1;
    $file = $2;

    if($ts)
    {
        $ts = str2time($ts);
    }
    else
    {
        warn("could not parse date on line:\n$item\n");
        next;
    }
    $files{$file}=$ts;
    $new_file = $ts > $new_ts ? $file : $new_file;
    $new_ts   = $ts > $new_ts ? $ts : $new_ts;
}

$ftp->get($new_file)
    or die "get failed ", $ftp->message;
$ftp->quit;
Reply With Quote
Google UNIX.COM
Reply

Tags
perl, perl regex, regex

Thread Tools
Display Modes




All times are GMT -7. The time now is 07:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0