2017-09-01 56 views

回答

1

在我看来,你可以使用基于年龄删除文件的标准方法,稍作修改以降低文件系统过满时的阈值。

删除所有*.thumb文件/tmp超过一定年龄(约一个月)的通常方法是用以下命令:

find /tmp -type f -name '*.thumb' -mtime +30 -delete 

所以,你需要做的是降低门槛是在某些情况下修改mtime测试。要做到这一点基于如何充分的文件系统可能会喜欢的东西来完成:

#!/usr/bin/env bash 

# Default to about a month. 

thresh=30 

# Get percentage used of /tmp, needs to match output of df, such as: 
# Filesystem  1K-blocks  Used Available Use% Mounted on 
# tmp    1000000 280000 720000 28% /tmp 

tmppct=$(df | awk '$6=="/tmp" { gsub("%", "", $5); print $5 }') 

# Reduce threshold if tmp more than 80% full. 

[[ ${tmppct} -gt 80 ]] && thresh=1 

# Go and clean up, based on threshold. 

find /tmp -type f -name '*.thumb' -mtime +${thresh} -delete 

的只是脚本传递的df(根据指定的格式)的输出通过的可能有点棘手:

awk '$6=="/tmp" { gsub("%", "", $5); print $5 }' 

这只是将:

  • 找其中第六字段是/tmp线;
  • 从第五个字段中删除尾部%;和
  • 最终输出(修改)的第五个字段来捕获完整的百分比。

然后,只需创建一个crontab条目,该条目将定期运行该脚本。

相关问题