tags:

views:

550

answers:

3

I need a shell or Perl script which would connect to the FTP server and delete all the files which are more than 7 days old.

cheers

+2  A: 

In Perl, you'd want to use Net::FTP's ls, mdtm, and delete commands.

ysth
Your link doesn't actually go to Net::FTP's documentation
Cebjyre
(edit) It does now.
pjf
+3  A: 

Use the Net::FTP module to connect to the ftp server as outlined in the CPAN document. To browse through the site listings you may have to combine cwd/cdup in order to handle directories (unless ofcourse all the files are in the root directory).

To get the file's modification time use the mdtm(FILE) method, just make sure to check if this is supported on the current server by calling

if( $ftp->feature( 'MDTM' ) ) {
   ...check modification time for file...
}

If not, then you might try calling the 'dir' method which will get you the listings in the long format, and then extract the date information from the individual file listings in order to compare and delete.

To compare the two dates use the Date::Calc module. The 'Delta_Days' method should give you the number of days between two dates; this can be used just as easily for either of the methods specified above.

muteW
+1  A: 

If it's a shell script you're after, you might be better off running a script in a crontab.

find /tmp -type f -mtime +7 -exec rm {} \;

alphabeat
How do you suggest he gets the find command on the ftp server?
muteW
He gets the find command the same way everyone else does: by logging into an interactive shell if he can.
brian d foy
Ah right didn't think of that muteW. Not used to not having shell access, I am.
alphabeat