Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Clear out old files in a directory

by snapdragon (Monk)
on Apr 18, 2001 at 15:04 UTC ( [id://73470]=CUFP: print w/replies, xml ) Need Help??

Brothers,

During my time as a system admin I have found Perl to be a most invaluble tool for all kinds of tasks. One of the best examples of this is trying to clean up old archive logs files from an Oracle database.

Archive log files are created from redo logs; however unlike redo logs archive logs are *not* cyclical. By this I mean that the redo logs will operate in a round robin mode, where the oldest is overwritten with new information. Archive logs, by contrast, keep on growing and can full up disk space if they are allowed to go unmonitored.

Because of this I have a little script below which will examine the file times of files in a directory and delete the files that are oldest (providing that there are more than 5 files in the directory).

I hope some other sys admins may find this useful.

#!/usr/bin/perl use strict; my ($dirname, $filecounter, $decision, $filename, $file, $WRITETIME); my (%list); $dirname = "/u01/oracle/admin/oraprod/arch//"; # where the ar +chived 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 $d +irname"; 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 $d +irname"; while (defined($filename = readdir(DIRHANDLE))) { if (!-d $filename) { # ignore . and .. ($WRITETIME) = (stat($filename)) [9]; $list{$filename} = "$WRITETIME"; # make hash for filena +mes 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"); } }

Replies are listed 'Best First'.
Re: Clear out old files in a directory
by merlyn (Sage) on Apr 18, 2001 at 15:49 UTC
    Hmm. The bourne shell really isn't going to like these:
    system("echo 'deleted file' >& /tmp/delete");
    And pretty much all your calls to system would be much better written just by opening a filehandle to /tmp/delete.

    -- Randal L. Schwartz, Perl hacker

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://73470]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (4)
As of 2024-03-29 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found