![如果同一目录中不同文件的数量超过30,则删除文件](https://linux22.com/image/92480/%E5%A6%82%E6%9E%9C%E5%90%8C%E4%B8%80%E7%9B%AE%E5%BD%95%E4%B8%AD%E4%B8%8D%E5%90%8C%E6%96%87%E4%BB%B6%E7%9A%84%E6%95%B0%E9%87%8F%E8%B6%85%E8%BF%8730%EF%BC%8C%E5%88%99%E5%88%A0%E9%99%A4%E6%96%87%E4%BB%B6.png)
该文件夹中填充了一些每天、每周或每月运行的作业。例如。日常工作产出如下:
daily-2017-16-08.txt
daily-2017-15-08.txt
每周就像:
weekly-2017-01-08.txt
weekly-2017-08-08.txt
每月就像:
monthly-2017-01-08.txt
monthly-2017-01-07.txt
答案1
像这样的东西吗?
#!/bin/bash
for FILE_TYPE in daily weekly monthly
do
COUNTER=0
for FILE in $(ls -t ${FILE_TYPE}-*)
do
let COUNTER++
[ ${COUNTER} -gt 30 ] && rm ${FILE}
done
done