#!/usr/bin/perl use strict; my ($dirname, $filecounter, $decision, $filename, $file, $WRITETIME); my (%list); $dirname = "/u01/oracle/admin/oraprod/arch//"; # where the archived files lives $filecounter = 0; # just a counter really $decision = 5; # how many files to keep $filename = ""; # call routines &ReadDir; # reads in directory &WriteTime; # write the time of the last command out if ($filecounter > $decision) { &GetStat; # finds out filetimes/deletes files } # sub WriteTime sub WriteTime { system("echo last file run was at `date` >> /tmp/delete"); } #sub ReadDir sub ReadDir { opendir(DIRHANDLE, $dirname) || die "Can't find or open directory $dirname"; while (defined($filename = readdir(DIRHANDLE))) { if (!-d $filename) { # ignore . and .. $filecounter++; # increment the counter } } closedir(DIRHANDLE); } sub GetStat { %list = (); # create a hash for the elements $filecounter = 0; # create a variable to hold the counter # open the directory and get all the VALID files out opendir(DIRHANDLE, $dirname) || die "Can't find or open directory $dirname"; while (defined($filename = readdir(DIRHANDLE))) { if (!-d $filename) { # ignore . and .. ($WRITETIME) = (stat($filename)) [9]; $list{$filename} = "$WRITETIME"; # make hash for filenames and time } } closedir(DIRHANDLE); # sort the hash by the associated values descending foreach $file (sort { $list{$b} <=> $list{$a} } keys %list ) { $filecounter++; if ($filecounter > $decision) { system("echo 'deleted file' >& /tmp/delete"); unlink("$dirname/$file"); } }