Operating System - HP-UX
1833323 Members
3307 Online
110051 Solutions
New Discussion

Re: Delete files from FTP

 
vas  bolpali
Advisor

Delete files from FTP

how delete the files on the FTP server ,
only if they are 15 days old ?

{
echo "open $SERVER
user $USER $PASS
ascii
cd temp
-- delete 15 days old files ?
close"
} | ftp -i -n

thnx in advance
keeping you ahead of the learning curve
9 REPLIES 9
John Poff
Honored Contributor

Re: Delete files from FTP

Hi,

I don't think you have any commands in the ftp server to list the files older than 15 days. What you might get away with is something like this:

open your connection and login
get a list of all the files
closing the connection

If you redirect the output from this session to a file, you can write a script to figure out which files you want to delete and build a script to drive a second ftp session to login and delete the files.

open connection and login
delete file
delete file
...
close


I don't know what your situation is, but it probably would be much easier to delete the old files from the ftp server end.

JP
Rajeev  Shukla
Honored Contributor

Re: Delete files from FTP

Hi,
You can not search for files older from ftp prompt. The best and easiest way would be to search and delete them from the ftp server side by putting some scripts and then triger the ftp from there instead from your end.

Thanks
Rajeev
A. Clay Stephenson
Acclaimed Contributor

Re: Delete files from FTP

Hi:

Just as an object lesson to you guys that say it can't be done and as another opportunity to bang you over the head with "USE PERL FOR FTP", I whipped this example up in under 5 minutes. As is, it logs in, cd's to /tmp, does an ls, and gets the last modification times of each file. If any are older than 15 days, it deletes them.

You will need to install the Net::FTP module from www.perl.org/CPAN. You will also need to uncomment the actual delete call when you get close.

Some suggested improvement:
1) Do a dir and only look for regular files but that is left as an exercise for the reader. 2) Add a bit of error checking - hard with scripts but duck soup with Net::FTP.

That's all you get for 5 minutes.

Smug regards,
Clay
If it ain't broke, I can fix that.
Sridhar Bhaskarla
Honored Contributor

Re: Delete files from FTP

Hi,

You can do it with quite a bit of scripting if you really want to do.

You may have to do it in multiple iterations

Your first part will get the details. This is a high level script.

#!/usr/bin

{
echo "open $SERVER
user $USER $PASS
ascii
cd temp
dir
close"
} | ftp -i -n > ftp.log

tough_part()
{
This ftp.log contains output with files and their date stamps. Here is the most difficult part. Find out the files in the output that are more than 15days (how?). Try caljd.sh floating around in these forums. Assign them to a variable say FILES.
Use ftp again to delete them using "mdelete"
}

{
echo "open $SERVER
user $USER $PASS
ascii
cd temp
mdelete $FILES
close"
} | ftp -i -n

Since it is tedious, I would suggest you to run a script through cron on the ftp server to clean the files.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
John Poff
Honored Contributor

Re: Delete files from FTP

Hi,

I didn't say it couldn't be done. My point was that it could be done with some scripting.

the humble JP


A. Clay Stephenson
Acclaimed Contributor

Re: Delete files from FTP

Hi John:

I trust that you know that was all tongue in cheek. This was simply an irrestible chance to illustrate what is at best tedious using shell, awk, grep, cut, ... becomes almost trivial when you choose the right club.

Humble (more or less) regards, Clay


If it ain't broke, I can fix that.
John Poff
Honored Contributor

Re: Delete files from FTP

Clay,

No problem. I was a little miffed when I first read it but I quickly got over it. It was late and I was tired. No offense taken. Besides, you were right. :)

I don't mind people being smug when they really, really know what they are doing! Please feel free to keep banging my hard head with good solutions. The owner's manual that came with me states, "Repeated percussive maintenance to the cranial region eventually yields positive results." ;)

the still humble JP

Jose Mosquera
Honored Contributor

Re: Delete files from FTP

Hi,

Try to this via a remote shell instead!
remsh -l -n find /temp/ -mtime +15 -exec rm {} \;

If your remote system isn't a remsh incompatible environment try via ftp by "literal" command.

Rgds.
A. Clay Stephenson
Acclaimed Contributor

Re: Delete files from FTP

I had a few spare minutes today and I went back and added a quick improvement. The ls method was replaced with the dir method (essentially an ls -l command) so that we can determine if this is a regular file. (Directories, pipes, symbolic links are skipped). If the first character of the first field of the dir listing is a '-' then we know the file is a regular file. The filename is the last field of the ls -l line.

Next we check to see if the file has been modified in the last 15 days. If not the file is deleted.

With only a little more work, you could make this a recursive function that would descend directories and continue the process but that is left as an exercise.

The power of this approach is that it could even be run from a Windows box without change. I never do any FTP stuff with Perl anymore; it's just too hard any other way especially if you care about minor little things like error checking.


If it ain't broke, I can fix that.