How to FTP the latest file, based on date, from a remote server through a shell script?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to FTP the latest file, based on date, from a remote server through a shell script?
# 1  
Old 03-07-2011
How to FTP the latest file, based on date, from a remote server through a shell script?

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.
# 2  
Old 03-07-2011
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).
This User Gave Thanks to kamalkishore For This Post:
# 3  
Old 03-07-2011
If Perl is acceptable:

Code:
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>


Last edited by radoulov; 03-07-2011 at 02:59 PM..
This User Gave Thanks to radoulov For This Post:
# 4  
Old 03-07-2011
Is "mmddyyyy" always today's date?
# 5  
Old 03-07-2011
This code will give you the most recent file based on "mmddyyyy" in the file name:
Code:
sed 's/\(.*_\)\(..\)\(..\)\(....\).txt/\4\2\3 &/' input_file | sort -r | head -1 | sed 's/.* //'

input_file:
Code:
build2_runtime_03042010.txt
buifile_count_06072010.txt
build3_runtime_04052010.txt
build1_runtime_05072010.txt

Output:
Code:
buifile_count_06072010.txt

This User Gave Thanks to Shell_Life For This Post:
# 6  
Old 03-08-2011
@ 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
Code:
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)
Code:
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

Last edited by radoulov; 03-08-2011 at 01:03 PM.. Reason: Code tags, please!
# 7  
Old 03-08-2011
List what you have and what they have, and using comm -23, download what is missing, gives auto recovery after outage.
This User Gave Thanks to DGPickett For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Ubuntu

FTP script to get latest file from server

Hello people, I have to download, with a scheduled script, the latest file from an FTP server. In the remote DIR, named .../TEKNONET/60468/, every night a CDR file like this gets uploaded into it: 000006046820151122N001.CDR, so my script will have to download every day the previous day... (12 Replies)
Discussion started by: virtus96
12 Replies

2. Shell Programming and Scripting

Download latest file via ftp server unix through shell script

Hello this is my first post in this forum , I dont want to be unhappy.. I am writing one script but facing difficulty to find the latest file with some new pattern My requirement is 1. The file is coming like "ABCD-23220140303" at FTP server once in a week. 2. script will run on daily... (3 Replies)
Discussion started by: ajju
3 Replies

3. Shell Programming and Scripting

Find the latest file based on the date in the filename

Hi, We've a list of files that gets created on a weekly basis and it has got a date and time embedded to it. Below are the examples. I want to find out how to get the latest files get the date and time stamp out of it. Files are PQR123.PLL.M989898.201308012254.gpg... (1 Reply)
Discussion started by: rudoraj
1 Replies

4. Shell Programming and Scripting

Needed shell script to get the latest file based on date

hi i need the shell script to get the file with latest date. for example in my input folder i have files like file_20130212.txt file_20130211.txt now my output folder should have the file with latest date i.e..file_20120212.txt i want to get the latest file .. i.e is should take... (6 Replies)
Discussion started by: hemanthsaikumar
6 Replies

5. Shell Programming and Scripting

FTP using script not working (for transfering file from a remote unix server to windows PC.)

hi, Im using the following code for FTP #!/usr/bin/ksh ftp -v -n "10.29.45.11" << cmd user "mahesva" "mahesva123" get rtl.tar quit cmd Below is the log when i run the above code ********************************** Connected to 10.29.45.11. 220 (vsFTPd 2.0.1) 530 Please login with USER... (20 Replies)
Discussion started by: dll_fpga
20 Replies

6. Shell Programming and Scripting

Script to upload latest file to other server via FTP

Hello, I have a script that finds the latest version of a file in a folder on my Minecraft server. I'm trying to come up with something that will then FTP that file over to my fileserver. Here's what I have that finds the newest file: find /home/mc/archive/sbhouse -type f -mtime +45 -exec... (7 Replies)
Discussion started by: nbsparks
7 Replies

7. Shell Programming and Scripting

copy the latest file in the remote server's directory

Hi Expert Team, I performed the below piece of code to copy the latest file in the remote server's directory to the same server's other directory. But it is not working properly. How can i handle this? Can you please help me..? ssh ${REMOTE_USERID}@${REMOTE_HOSTNAME} "cp -p `ssh... (3 Replies)
Discussion started by: spkandy
3 Replies

8. Shell Programming and Scripting

grep latest file based on date.

hi all, not sure if this has been posted b4 but i try to search but not valid. this is my question: when i do a ls -ltr there will be a list generated as follows: -rw-r--r-- 1 root sys 923260 Jan 10 04:38 FilePolling.41025.083TL021.xml -rw-r--r-- 1 root sys 1761337 Jan 10 04:40... (12 Replies)
Discussion started by: lweegp
12 Replies

9. Shell Programming and Scripting

Shell script to ftp latest file

Hello All, Hope u r doing fine. I'm writing a shell script to ftp the latest file having pericular convention as 'ULTI_15072007043205.txt' on daily basis. Now the date & timing of the file geleration isnt constant, so the file name daily varies as per the date & timing of generation. Can anyone... (7 Replies)
Discussion started by: im_new
7 Replies

10. Shell Programming and Scripting

FTP - Get the file date and time on the remote server

I would like to know if there is a way to get the date and timestamp of the file that is being FTP from the remote server using shell script. Currently the get command from FTP will have current date and timestamp. Tried the earlier suggestion 'HardFeed' but still getting the current date and time... (12 Replies)
Discussion started by: gthokala
12 Replies
Login or Register to Ask a Question