庆典:删除所有文件早于1个月,但周一留下的文件 [英] bash: Delete all files older than 1 month, but leave files from Mondays

查看:99
本文介绍了庆典:删除所有文件早于1个月,但周一留下的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有很多日常备份存档。要管理磁盘使用情况,我需要一个bash脚本,将删除所有文件早于1个月,但保持在星期一创建的所有文件,即使他们是年龄超过1个月。

I have a lot of daily backup archives. To manage disk usage, I need a bash script that will delete all files older than 1 month, but keep all files created on Mondays, even if they are older than 1 month.

例如,这将删除所有文件最后修改时间超过30天前:

For example, this will delete all files last modified more than 30 days ago:

 find /path/to/files* -type f -mtime +30 -delete

但我真的不知道如何保持在星期一创建的文件。

But I don't really know how to keep files created on Mondays.

推荐答案

稍微简单一些,更谨慎的版本@常总的回答是:

Slightly simpler and more cautious version of @JoSo's answer:

find /path/to/files -type f -mtime +30 \
    -exec sh -c 'test $(date +%a -r "$1") = Mon || echo rm "$1"' -- {} \;

的区别:


  • 使用日期-r 来获取文件直接
  • 的最后修改日期
  • 使用%A 与多个COM prehensible平日名工作

  • 只是呼应了 RM$ 1先回顾一下将被删除。如果看起来不错,然后要么固守 | SH 末真正执行或删除回声

  • Using date -r to get the last modification date of a file directly
  • Using %a to work with more comprehensible weekday names
  • Just echo the rm "$1" first to review what will be deleted. If looks good, then either stick | sh at the end to really execute, or remove the echo

不过,@JoSo是正确地指出,日期+%A 依赖于语言环境,所以这些版本会确实更安全:

However, @JoSo is right to point out that date +%a is locale dependent, so these versions would be indeed safer:

find /path/to/files -type f -mtime +30 \
    -exec sh -c 'test $(date +%u -r "$1") = 1 || echo rm "$1"' -- {} \;
find /path/to/files -type f -mtime +30 \
    -exec sh -c 'test $(LC_TIME=C date +%a -r "$1") = Mon || echo rm "$1"' -- {} \;

这篇关于庆典:删除所有文件早于1个月,但周一留下的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆