
Borderline
Deleted
Jan 13, 2000, 6:33 PM
Post #6 of 6
(7014 views)
|
Re: How do I delete a file when diskfull?
[In reply to]
|
Can't Post
|
|
Hi, Here is something that may help. Please understand this is not tested code... <BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR> #!/usr/bin/perl -w use strict; use vars '%size'; # This is a lazy way to recurse and # you may have to install this module use File::Recurse; open DF, "/bin/df|" or die "Broken pipe: $!"; my $used; while (<DF> ) { if (m| (\d+)% +/log$|) { $used = $1; last; } } close DF or warn "Pipe closed prematurly: $?"; if ($used >= 90) { opendir LH,'/log' or die "Could not open /log: $!"; my @logs = grep /\d{8}/, readdir LH; closedir LH; # I do not know of a way to get the size of an # entire directory in perl without recursing it. # There may be a way I do not know of. The two # ways I know are to either recurse it our self # or use /usr/bin/du. In this example we will # do it our self # If someone knows a better way to do this # please let me know recurse(\&add,"/log/$_",$_) for @logs; sub add { $size{$_[1]} += (-s) if (-f) } my @sortedkeys = sort { $size{$a} <=> $size{$b} } keys %size; # OK now we need to remove the directory recursivly # again I know of no built in way to do this so we # can either recurse is our self or use /bin/rm -r # Here I will use /bin/rm -r sence it is a common program system('/bin/rm','-r',"/log/$sortedkeys[0]") == 0 or die "System /bin/rm -r /log/$sortedkeys[0] failed: $?"; } exit;</pre><HR></BLOCKQUOTE> Please Please test this before you use it. Scott
|