我如何保留最新的8个备份文件并删除较旧的文件
我如何保留最新的8个备份文件并删除较旧的文件,
backup-Y-M-D.zip
backup-Y-M-D.zip
backup-Y-M-D.zip
backup-Y-M-D.zip
.
.
backup-Y-M-D.zip
大约有80个文件具有.zip扩展名。我要做的就是根据创建的日期保留最新的8个文件。我还尝试了logrotate,但没有旋转日志,因为它没有做任何事情。下面是logrotate的配置文件。
/root/test/*.zip {
daily
missingok
extension .zip
rotate 4
nocompress
}
How do I keep the latest 8 backup files and delete the older one
backup-Y-M-D.zip
backup-Y-M-D.zip
backup-Y-M-D.zip
backup-Y-M-D.zip
.
.
backup-Y-M-D.zip
There are about 80 files having .zip extension all I wanted to do is to keep latest 8 files according to the date on which created. I also tried logrotate but failed to rotate logs as it is not doing anything. Below down is the config file of logrotate.
/root/test/*.zip {
daily
missingok
extension .zip
rotate 4
nocompress
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果保证了命名约定,则可以在扩展Glot模式以获取最古老或最新文件时仅依靠文件的字母顺序排序。根据 fileName扩展:
演示:
您可以利用它以获取除最后一个n元素以外的文件列表:
然后在该列表中进行任何您想做的任何您想做的事情,例如使用
rm删除它>。
If the naming convention is guaranteed you could just rely on the alphabetical ordering of the files when expanding a glob pattern to get the oldest or newest files. According to Filename Expansion:
Demo:
You can leverage this to get a list of files other than the last N elements:
And then do whatever you want with that list, such as remove it with
rm "${old_files[@]}"
.一种方法是使用以下单线仪,从日志所在的目录中运行:
说明:
ls -t -t
- 列出所有文件,以从最年轻到最古老的hear -n -8
- 获取所有文件,除了第一个8xargs -no -run -if -if -if -if -if -if -if -if -if -if -if -if-code
- 如果有的话,请删除所选文件,如果您曾经,请阻止错误 如果您想设置此数字少于8个日志,以便每天自动运行,以防您的服务器在周期的第七天离线并错过一周标记,运行
crontab -e
并将以下内容添加到您的工作中:然后,日志清洁器每晚都会在午夜运行。
One way to do this is with the following one-liner, ran from the directory where the logs are located:
Explanation:
ls -t
- lists all the files in order from youngest to oldesthead -n -8
- gets all the files except the first 8xargs --no-run-if-empty rm
- deletes the selected files if there are any, preventing errors if you ever have fewer than 8 logsIf you want to set this up to run automatically every day, giving you peace of mind in case your server is offline on the 7th day of a cycle and misses the one week mark, run
crontab -e
and add the following to your jobs:Then the log cleaner will be ran every night at midnight.