The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 4 04-05-2009 01:14 AM
get latest file inquirer Shell Programming and Scripting 4 04-02-2009 11:15 AM
Read the latest file wereyou UNIX for Dummies Questions & Answers 3 02-10-2008 12:12 AM
get latest file via ftp command alx Post Here to Contact Site Administrators and Moderators 2 01-26-2006 11:44 AM
How to Grab the latest file n9ninchd UNIX for Dummies Questions & Answers 1 05-10-2001 04:31 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-08-2008
arunavlp arunavlp is offline
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
  #2 (permalink)  
Old 02-08-2008
GaneshB GaneshB is offline
Registered User
  
 

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

ls -ltr
  #3 (permalink)  
Old 02-08-2008
arunavlp arunavlp is offline
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
  #4 (permalink)  
Old 02-08-2008
HPAVC's Avatar
HPAVC HPAVC is offline
Registered User
  
 

Join Date: Feb 2008
Posts: 106
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;
Sponsored Links
Closed Thread

Bookmarks

Tags
perl, perl regex, regex

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:43 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0