Get the most recent file from a remote server

Hi,
This is an FTP related query.I hvae 2 files in a remote server with the timestamp attached to it.
FilenameYYYYMMDDHHMMSS.EXT.
I need to extract both the fileswith the most recent timestamp using the FTP script
I have used :

ftp Servername
blah blah....

MOST_RECENT_FILE1=`ls -1t ${FTP_OUTPUT_DIR}${FTP_OUTPUT_FILE1}* | head -1 `
get ${MOST_RECENT_FILE1} ${FTP_INPUT_DIR}${FTP_INPUT_FILE1}.EXT

The parameter names speak for themselves.

I am unable to extract the most recent file using ls and head and i get the message FILE NOT FOUND.
Is there an alternative ?

Thanks,

one way:

  1. use either ftp's 'ls' or 'dir' to spool the listing of file in a given remote directory to a local file
  2. 'massage' the listing of the remote files on the local machine - using UNIX utilities to determine the 'newest' file based on the filename time encoding
  3. once the 'newest' file is determined - reconnect to the remote machine and 'get' it.

Thanks for the reply.
I did not understand what you mean by 'massage the listing'.
there are numerous files in the remote directory and I cannot get all of them into the local machine and then pick the recent one.
I need to pick the recent file in the remote and get only that file.
Please let me know if you have any questions.

doing 'man ftp' yields the following for the ftp's "ls" command:

     ls [ remote-directory | -al ] [ local-file ]
           Prints an abbreviated listing of  the  contents  of  a
           directory  on  the remote machine. If remote-directory
           is left unspecified, the current working directory  is
           used.

           The -a option lists all entries, including those  that
           begin  with  a dot (.), which are normally not listed.
           The -l option lists files in long format, giving mode,
           number of links, owner, group, size in bytes, and time
           of last modification for each file. If the file  is  a
           special  file,  the  size  field  instead contains the
           major and minor device numbers rather than a size.  If
           the  file  is a symbolic link, the filename is printed

get the 'listing' of the direcotry on the 'remote' host.
spool that listing to the 'local' machine's file.
'massage' the listing on the local machine to determine the 'newest' file.
ftp 'get' the "newest" file from the remote machine.

Got it.
But it is a pretty crude method.
Liste the files on the local machine.
sorted and got the most recent file.
Again FTP'ed to the remote server and did a 'get' to get the recent file.
I wonder if there is a way to do it in one shot.

You don't have to break the ftp connection. You can use the "!" to run shell commands on your local machine when you determine the latest file.