I wrote the following in PHP but I was wondering if there is an elegant way to do this in a Linux shell script? Basically delete files over (n) days old, but leave the (n) newest files regardless of age.
PHP
foreach (glob("backup/*.db") as $file) {
$a[$file]=date("Y-m-d",filemtime($file));
}
$i=0;
arsort($a);
foreach($a as $file=>$date) {
if ($i++>=10) {
if ($date<=date("Y-m-d",strtotime("-10 days"))) {
unlink($file);
xmessage("PURGED: $file");
}
}
}
My idea was to delete with "find -mtime +(n) exec rm" but only pipe in the files that are NOT in "head -n +(n)"? But "head -n" does not seem to do what I thought it would. Thanks.
SHELL SCRIPT
find -mtime +10 | ls -t *.DB.tar.gz | head -n -10
Try this using all GNU find, sort, awk, and xargs:
Change
ls
torm
when you're done testing and sure it's giving you the expected output.