How to FTP the latest file, based on date, from a remote server through a shell script?
I have four files to be FTP'ed from remote server.
They are of the following format.
build1_runtime_mmddyyyy.txt
build2_runtime_mmddyyyy.txt
build3_runtime_mmddyyyy.txt
buifile_count_mmddyyyy.txt
This is how I am getting all the files form remote server.
But, I need to get the latest files (four files coming each day), based on date.
When a ftp is done, it may not be possible to list the files based on time (creation).
In my opinion best bet is if you can create the file name by appending the date and then ftp(pull) the file(as you say the files come each day).
1 Like
If Perl is acceptable:
perl -MNet::FTP -e'
($host, $user, $pass) = @ARGV;
$ftp = Net::FTP->new($host) or die "$@\n";
$ftp->login($user, $pass) or die $ftp->message;
$ftp->get(
(map $_->[1],
sort {
$b->[0] <=> $a->[0]
} map [
$ftp->mdtm($_),
$_
], $ftp->ls
)[0]
) or die $ftp->message;
$ftp->quit or die $ftp->message;
' <host> <user> <pass>
1 Like
methyl
March 7, 2011, 12:52pm
4
Is "mmddyyyy" always today's date?
This code will give you the most recent file based on "mmddyyyy" in the file name:
sed 's/\(.*_\)\(..\)\(..\)\(....\).txt/\4\2\3 &/' input_file | sort -r | head -1 | sed 's/.* //'
input_file:
build2_runtime_03042010.txt
buifile_count_06072010.txt
build3_runtime_04052010.txt
build1_runtime_05072010.txt
Output:
buifile_count_06072010.txt
1 Like
@ radoulov I want in Shell script
@ methyl mmddyyyy will be previous date..(The day before)
---------- Post updated 03-08-11 at 12:01 PM ---------- Previous update was 03-07-11 at 03:32 PM ----------
@ Shell_life Thank you very much for your reply but my requirement is
My source folder has following files(i will get 4 files everyday and I need to ftp the files files which came in today)
On 03/07/2010 I will receive files with date 03062010. I need to ftp all the four different kinds files which come in on 03072010
SOURCE
build1_runtime_03042010.txt
build2_runtime_03042010.txt
build3_runtime_03042010.txt
buifile_count_03042010.txt
build1_runtime_03052010.txt
build2_runtime_03052010.txt
build3_runtime_03052010.txt
buifile_count_03052010.txt
build1_runtime_03062010.txt
build2_runtime_03062010.txt
build3_runtime_03062010.txt
buifile_count_03062010.txt
TARGET(i need to ftp these files in target folder)
build1_runtime_03062010.txt
build2_runtime_03062010.txt
build3_runtime_03062010.txt
buifile_count_03062010.txt
Could you please help me with this.
Thanks
Please
List what you have and what they have, and using comm -23, download what is missing, gives auto recovery after outage.
1 Like
@DGPickett I do not want all the files in the target folder, I Just need latest set of files(4 files)
methyl
March 8, 2011, 12:46pm
9
First a bit of preparation.
Create a shell script containing say:
echo "YESTERDAY=\"`date +'%m%d%Y'`\"" > /var/tmp/yesterday
chmod 755 /var/tmp/yesterday
Then create a cron for 23:59 daily to run this script.
The script generates a line like:
YESTERDAY="03082011"
The following day.
Next bit is a vague because we don't know anything about your existing FTP script.
Your script will then probably contain:
. /var/tmp/yesterday # Set $YESTERDAY to yesterday date
And in the FTP commands in your Shell here document:
mget *${YESTERDAY}.txt
Yes, but if them make one file a day and you were in syn after your run yesterday, that is one file. If there is a problem, you might move more than one, and then you are back in sync. Otherwise, you have to parse ftp 'ls' output for date-time and sort to find the newest file. The 'comm -23' way is robust and simpler -- no parse, platform independent, name independent.