in reply to Deleting Oldest Logfiles
use strict; use warnings; sub partition_usage { my $partition = shift; my $line = (`/bin/df -B 1024 $partition`)[-1]; return 1024 * (split(/\s+/, $line))[2]; } # test it: print partition_usage('/home/'), "\n";
Then you should get a list of the log files you might want to delete. Then sort them by modification time. This example assumes that they are all in the current directory:
my @files = glob 'logfile_*.log.gz'; @files = reverse sort { -M $a <=> -M $b } @files.
The the documentation of -M and sort for more details.
Then it's just a matter of walking through these files, and delete until you no longer exceed your size limit:
my $limit = 10 * 1024**3; # 10 GB for my $filename (@files) { last if partition_usage('/var/log/') < $limit; unlink $filename or warn "Can't delete file '$filename': $!"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Deleting Oldest Logfiles
by longjohnsilver (Acolyte) on Oct 23, 2008 at 11:44 UTC | |
by moritz (Cardinal) on Oct 23, 2008 at 11:49 UTC | |
by longjohnsilver (Acolyte) on Oct 24, 2008 at 07:18 UTC |