I have a remote server that generates files with name format:
Daily_Generated_File_11-14-07.txt.
The file name remains the same regardless, but the date portion at the end of the name will change (mm-dd-yy). Can someone supply a script that will search for files with the latest date based on the file name?
I need the script to look in a remote directory for today's files. I came up with this and it works, but now how to I get this to run on a remote FTP Server
FILENAME="Daily_Generated_File_$(date +%m-%d-%y).txt"
find . -type f -name $FILENAME
#!/bin/ksh
ftp -nv <<EOF
open remoteServer
user userName userPassword
cd desiredDirectory
mls Daily_Generated_File_* myLocalListingFile
EOF
# once you get the 'myLocalListingFile' on the LOCAL server, go through files listed in the
# file and decide which one is the latest.
# Once decided, ftp to the remote server and get the desired file
ftp -nv <<EOF
open remoteServer
user userName userPassword
cd desiredDirectory
get desiredLatestFile
EOF
Thanks for the update. Here's another change: Apparently the files that are generated are a day behind, so today's file 12-19-07 won't be available until 12-20-07. The script you so graciously gave me, will only look for today's file. How can the script be edited to look for yesterdays file? I'm assuming it would be in the
Hi,
This one is a little difficult. But should work for you.
1> find . -name Daily_Generated_File_\*
---> find all the related file in current directory
2> sed 's/\(Daily_Generated_File_\)\([0-9][0-9]\)\(-[0-9][0-9]-\)\([0-9][0-9]\)\(.txt\)/\1\4\3\2\5/'
---> since the file is in MM-DD-YY format, change it to YY-MM-DD format
3> sort -t -
---> sort the result with '-' as delimite
4> tail -1
---> get the last one which is the latest according to the date
5> sed 's/\(Daily_Generated_File_\)\([0-9][0-9]\)\(-[0-9][0-9]-\)\([0-9][0-9]\)\(.txt\)/\1\4\3\2\5/'
---> change from 'YY-MM-DD' back to 'MM-DD-YY'
The script 'gets' the LAST file (sorted by 'date'). If your file generation lags a day, then the 'last' available file will from yesterday (assuming files are generated on the daily bases with no gaps).
Unless I'm missing something obvious.....
You're right. But when I ran the script, it kept coming back with nothing found. Then it hit me "duh! if it's looking for today's file then..." so i changed the date on one of the files in the dir to todays file and ran the script again with the "echo" and it spit out the file with today's date.
Hm..... I'm not sure if it's good or bad.....
There's nothing in the script that deals with either today OR yesterday.
As I said previously - the script gets the LAST file it finds (sorted by the 'date').
Not sure if everything is kosher now or not.........