Skip to content

Instantly share code, notes, and snippets.

@javier-lopez
Created September 25, 2017 16:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save javier-lopez/e9649d98c4ca4e28565f8b972fa870db to your computer and use it in GitHub Desktop.
Save javier-lopez/e9649d98c4ca4e28565f8b972fa870db to your computer and use it in GitHub Desktop.
keep n more recent files in directory
#!/usr/bin/perl
#description: keep n more recent files in directory
#usage: keepn DIR <n>
#exampe: keepn /var/log/apt/ 15 #default to 10
#keep latest 15 files, remove the rest
use strict;
use warnings;
use File::Basename;
my $progname = basename(__FILE__);
my $default_n = 10;
sub _usage {
print "Usage: " . $progname . " DIR <n>\n";
print "Keep n more recent files in directory.\n\n";
print " -h, --help show this help message and exit\n";
}
sub _die {
print STDERR "@_\n" if (scalar(@_) > 0);
select STDERR; _usage(); exit 1;
}
sub _keepn {
my ($dir, $n) = @_;
$n = $default_n if not defined $n;
if ( ! -d $dir) {
_die("Directory '" . $dir . "' doesn't exists!");
}
my @newest_first =
map $_->[0],
sort { $a->[1] <=> $b->[1] }
map [$_, -M],
< $dir/* >;
unlink @newest_first[$n..$#newest_first]
if @newest_first > $n;
}
_die() unless @ARGV;
foreach (@ARGV) { #parse options
($_ eq '-h' || $_ eq '--help') && do { _usage() && exit };
($_ =~ /^-./) && do { _die("$progname: unrecognized option '$_'") };
}
_keepn @ARGV;
@javier-lopez
Copy link
Author

javier-lopez commented Sep 25, 2017

Cron usage:

* * * * * /path/to/keepn   /path/to/logs/directory  100 #keep latest 100 files, delete all others

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment