我用 PHP 编写了以下内容,但我想知道是否有一种优雅的方法可以在 Linux shell 脚本中执行此操作?基本上删除超过 (n) 天的文件,但保留 (n) 最新的文件,无论年龄大小。
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");
}
}
}
我的想法是用“find -mtime +(n) exec rm”删除,但只在不在“head -n +(n)”中的文件中使用管道?但是“head -n”似乎并没有像我想象的那样做。谢谢。
SHELL SCRIPT
find -mtime +10 | ls -t *.DB.tar.gz | head -n -10