Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GET most recent file

Status
Not open for further replies.
Oct 16, 2001
7
US
Everyday we have to ftp to a remote site and GET the latest file containing order information. Each day a new file is added to the directory e.g. LAN0021.file on Monday, LAN0002.file on Tues etc.

we are looking to automate this ftp process but with the file to GET changing every day, is there a way to GET the most recent file?

any suggestions would be appreciated.

thanks in advance
 
Suggestion:
Make a variable as 'FILEDAY=`date +%a`' in a script. Use this variable to control the ftp gets which folder's file.

If you use 'mget', then you may not concerned the file name. But, if you use 'get' for one file. Do you know the structure of the file name? Do the number is random? If you just want the most recent file and don't know the number stand for. Then I have a question on ftp. Because I can't list file and sort it by time in ftp, is it true? If yes, one stupid solution is 'mget' all files and you may restrict the range as 'mget LAN00*.file'. Then sort the files(In local) and remove the files except the latest one.

tikual
 
You could connect to the FTP site twice, the first time to get a directory listing. Then locally you could sort that listing and pull out the latest file (it will probably be the last one listed anyway if the file names are sequential like that). Then connect to the FTP site again to download it.

Annihilannic.
 
the numbers are sequential with each day. when I do a file listing the files are listed by modification date so the last file (most recent - the one I need) is listed last. Is there some sort of "get last" command?
 
I'm afraid not... the commands available in FTP are pretty basic.

Try something like this:

Code:
#!/bin/ksh

ftp -nv remotehost << HERE > /tmp/ftp.out
user username password
cd /tmp
dir
HERE

# This part gets the last filename from the directory
# listing by ignoring lines that begin with a number
# (as they are FTP messages) and printing the last field
# on the last line.
FILENAME=$(awk '!/^[0-9]/ {print $NF}' /tmp/ftp.out | tail -1)

ftp -nv remotehost << HERE > /tmp/ftp2.out
user username password
cd /tmp
bin
get $FILENAME
HERE

Obviously change remotehost, username, password and /tmp to the correct values for your systems.

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top