automated ftp.

Hi
I am trying to delete some specific files ( files other than created today) from the server on a cron basis. I wrote a small script, but I am stuck up in how to delete only specific files.

#!/usr/bin/expect -f

set IP [lindex $argv 0]
set timeout -1
spawn ftp $IP
expect ):
send "username\n"
expect word:
send "password\n"
expect ftp>
send "prompt\n"
expect ftp>
send "passive\n"
expect ftp>
send "cd logs/\n"
expect ftp>

    After this I am stuck up. It is because I want to delete files with old dates ie, I don't want to delete the file dated today.

The files are like logfile-070501,logfile-070502 and so on..

    I cannot run the command "rm logfile-0705*"  \( as it will delete today's file too\)

By the way, I have the list of files to be deleted in a separate file. I am wondering if by running any other additional script, we can delete selected files from the remote server .

   I tried with ! command of ftp, but it runs the commands on local machine.

    Your help will be highly appreciated.Thanks

sangfroid,
See if this would work for you:

vYYMMDD=`date '+%y%m%d'`
vFName='logfile-'$vYYMMDD
rm -f `ls -1 log* | egrep -v "$vFName"`

i cannot execute rm -f inside ftp session and neither can i run egrep in it.

Another option is to prepare a file with all the deletes, adding "delete"
in front of each file name in your list:

sed 's/^/delete /' list_file_names > to_del_file_names

Would this work for you?

I am getting you. But the problem is how to execute at the remote end "inside" ftp session...

i am stuck up in this one

sangfroid,
Here is a solution:

rename logfile-070503 hold_logfile-070503
delete logfile*
rename hold_logfile-070503 logfile-070503

you could use co-process something like this:

Click Here

Again the problem is i cannot move the log file of today , as it is being updated continuously ( a live server)
the second problem is to evaluate date once the expect script runs...

anyway , i think i need to delete individual files by a bash script...login , delete file, login again and so on...but very inefficient.

Thanks a lot for your precious time and help. Thank you very much :slight_smile:

Here is an example of something I did that is similar to your request:

ftp -in ftp.site.com <<End-Of-Session
user user_id pass_word
type ascii
cd outbound
lcd /law1/lawson/logan/OWENS/temp
mget *
quit
End-Of-Session
cat /law1/lawson/logan/OWENS/temp/* > /law1/lawson/logan/OWENS/OM810
if [[ -s /law1/lawson/logan/OWENS/OM810 ]]; then
   echo 'ftp -in ftp.site.com <<E-O-F' > /law1/lawson/logan/edi_bin/om810ftp
   echo 'user user_id pass_word' >> /law1/lawson/logan/edi_bin/om810ftp
   echo 'type ascii' >> /law1/lawson/logan/edi_bin/om810ftp
   echo 'cd outbound' >> /law1/lawson/logan/edi_bin/om810ftp
   cd /law1/lawson/logan/OWENS/temp/
   ls | while read filename
   do
     echo 'delete ' $filename >> /law1/lawson/logan/edi_bin/om810ftp
   done
   echo 'quit' >> /law1/lawson/logan/edi_bin/om810ftp
   echo 'E-O-F' >> /law1/lawson/logan/edi_bin/om810ftp
   . /law1/lawson/logan/edi_bin/om810ftp
   rm /law1/lawson/logan/OWENS/temp/*
   cp /law1/lawson/logan/OWENS/OM810 /law1/lawson/logan/edi_arch_inb/OM810.inb."$(date +%Y%m%d%H%M%S)"
else
    echo "File OM810 not found or is empty"
	return 12
fi
exit

I ftp into the site and grab all the files. I then merge them into one file. I check to see if the one file is there which means that There were files on the remote site and if the one file is there I create an ftp script that will delete all of the files at the remote site. I then execute the script, delete any files on the local machine I no longer need and archive the one file. This is not exactly what you want, but I thought it might give you some ideas. Good luck!

Hi
after the first line, it stuck up..

ftp -in ftp.site.com <<End-Of-Session
user user_id pass_word

also it is not accepting the username and password this way.

Did it work in your case ?

Yes it runs daily, but not from cron. I have an application the kicks it off. Are you able to access the ftp site if you try from the command line? This script probably will not work exactly for what you want, but I thought it might give you some ideas for your own script.