Operating System - Linux
1830242 Members
2313 Online
109999 Solutions
New Discussion

ftp script with file date and time selection

 
SOLVED
Go to solution
Kasman_1
Occasional Contributor

ftp script with file date and time selection

Hi,

We have a file generated by the cron job every 5 minutes past of certain hour during one day time period everyday.

And now we need to ftp those files after they has been generated by the cron job, at the same time we do not want to ftp those files that we have already ftp(ed) before, so we need only to ftp the latest file generated.

The format of the file is as followed :

HOSTNAME_YYMMDDHHMM.gz

So in the directory we have the folowing bunch of files

NHU2CG1_0703140905.gz
NHU2CG1_0703141205.gz
NHU2CG1_0703141605.gz
NHU2CG1_0703141905.gz
.
.
.

Can someone advise how to write the ftp script to transfer those file according the the requirement mentioned above ?

Many thanks
5 REPLIES 5
Dennis Handly
Acclaimed Contributor

Re: ftp script with file date and time selection

You need to do several things.

1) First you have to ftp one file. Have you found how to do that?

2) The second thing you need to do is to select which files to do. The simplest way to do that is to use touch -r to set a reference file. The first time you want to either set it to the last file you did manually, or to some arbitrary time last year.

Then you can use: find NHU2CG1_*.gz -newer reference-file.

You would want to send all those files to the script/fragment you created for 1) above.

3) In the above process, you need to sort those files so you can set the reference file to the last one you copied.

Now you are all set to do this later.
Kasman_1
Occasional Contributor

Re: ftp script with file date and time selection

Hi,

Please pardon for my ignorant as I have never written any shell script before.

So would appreciate if someone can give some sample script that has been written before and I will try to modify according to my environment.

Thanks !
Peter Godron
Honored Contributor
Solution

Re: ftp script with file date and time selection

Hi,
unless you insist on using ftp, why don't you use things like rdist and rsync?

Otherwise, please see:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=991609

Please also read:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33 on how to reward any useful answers given to your questions.

Your profile shows you only awarded points to 1 of 15 answers !
Dennis Handly
Acclaimed Contributor

Re: ftp script with file date and time selection

>So would appreciate if someone can give some sample script that has been written before and I will try to modify according to my environment.

See Peter's link on how to do ftp. Here is one way to do the selection:

# Create reference file, with date of Jan 1, 1970, if not there
if [ ! -f NHU2CG1_.ref ]; then
    touch 0101000070 NHU2CG1_.ref
fi

find NHU2CG1_??????????.gz -newer NHU2CG1_.ref | sort |
while read file; do
    echo ftp_one_file $file # ftp this one
    touch -r $file NHU2CG1_.ref
done

I'm assuming the names of the files are created based on the times, and the above ascii sort matches the time sort of ll -t.

The "echo ftp_one_file" place is where you would put your script to ftp one file.

Peter Nikitka
Honored Contributor

Re: ftp script with file date and time selection

Hi,

get a listing of the available remote files and loop of this list to sort out the local available:

cd localdir
# setup your ftp connection
ftpini='_init_commands_
cd remote'

ftp remote <$ftpini
ls . remote.lst
bye
EOF

while read entry
do
[ -z "$entry" ] && continue
[ -f $entry ] && continue
print get $entry
done getme.lst

if [ -s getme.lst ]
then
ftp remote<$ftpini
bin
$(bye
EOF
fi

You'll have to test this ...

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"