【发布时间】:2019-04-16 17:13:47
【问题描述】:
我有一个文件夹,其中包含过去 15 天的数据库备份文件。 我需要删除所有文件,每天只保留一个,一个三天零一周。 有人可以帮忙吗?
我尝试了一些脚本,但没有一个符合我的要求
mkdir -p monthly
mkdir -p weekly
ln backup_$NOW.tgz weekly/
# find current month
month=$(date +%Y-%m-)
# find the first file of the current month in the weekly folder
first_monthly=$(ls --sort=time -1 weekly/*$month* 2>/dev/null | tail -1)
# and put it in the monthly folder
ln -f $first_monthly monthly/
# we need only 5 weekly backups
ls --sort=time -1 weekly/* 2>/dev/null | tail -n+6 >> /tmp/deletebackups.txt
# we need only 5 monthly backups
ls --sort=time -1 monthly/* 2>/dev/null | tail -n+6 >> /tmp/deletebackups.txt
# delete the extra files
#rm $(cat /tmp/deletebackups.txt) 2>/dev/null
xargs --arg-file /tmp/deletebackups.txt rm
【问题讨论】:
标签: bash powershell