通过删除旧文件来限制目录的大小

我有一个IP摄像头,它将其录像保存在我的Ubuntu Server 12.04中名为Camera1的特定目录中。

我想通过每天删除一次 – 最旧的文件来将此文件夹的大小限制为5 gigs。

我首先检查了配额程序,但它似乎不允许创建新文件和删除旧文件。

所以我认为最好的方法是运行bash脚本……

find命令可用于查找文件并删除它们。 例如,以下命令将删除七天以前创建的文件:

 find /path/Camera1 -ctime +7 -delete 

如果要安排,请使用crontab ; 这里有更多的信息 。

我情况非常相似。 我有一个包含2个目录的大型LVM分区:

  • video(录制的直播流)可以使用88%的分区

  • 图片(检测到动作时相机发送的图片)可以使用7%的分区(因为图片更轻,我仍然可以保留比video更旧的图片)

剩余的5%是安全边际,因此分区永远不会满。 注意我说的是百分比而不是固定千兆字节,因为如果我更换或添加硬盘驱动器,我的LVM分区的大小会发生变化。

所以这是脚本(我添加了许多注释,因此很容易理解):

 #!/bin/bash #Usage = sh limit-directory-size.sh /media/computer/mypartition 88 120 ("/media/computer/mypartition" = the directory to be limited / "90"=the percentage of the total partition this directory is allowed to use / "120"=the number of files to be deleted every time the script loops (while $Directory_Percentage > $Max_Directory_Percentage) #Directory to limit Watched_Directory=$1 echo "Directory to limit="$Watched_Directory #Percentage of partition this directory is allowed to use Max_Directory_Percentage=$2 echo "Percentage of partition this directory is allowed to use="$Max_Directory_Percentage #Current size of this directory Directory_Size=$( du -sk "$Watched_Directory" | cut -f1 ) echo "Current size of this directory="$Directory_Size #Total space of the partition = Used+Available Disk_Size=$(( $(df $Watched_Directory | tail -n 1 | awk '{print $3}')+$(df $Watched_Directory | tail -n 1 | awk '{print $4}') )) echo "Total space of the partition="$Disk_Size #Curent percentage used by the directory Directory_Percentage=$(echo "scale=2;100*$Directory_Size/$Disk_Size+0.5" | bc | awk '{printf("%d\n",$1 + 0.5)}') echo "Curent percentage used by the directory="$Directory_Percentage #number of files to be deleted every time the script loops (can be set to "1" if you want to be very accurate but the script is slower) Number_Files_Deleted_Each_Loop=$3 echo "number of files to be deleted every time the script loops="$Number_Files_Deleted_Each_Loop #While the current percentage is higher than allowed percentage, we delete the oldest files while [ $Directory_Percentage -gt $Max_Directory_Percentage ] ; do #we delete the files find $Watched_Directory -type f -printf "%T@ %p\n" | sort -nr | tail -$Number_Files_Deleted_Each_Loop | cut -d' ' -f 2- | xargs rm #we delete the empty directories find $Watched_Directory -type d -empty -delete #we re-calculate $Directory_Percentage Directory_Size=$( du -sk "$Watched_Directory" | cut -f1 ) Directory_Percentage=$(echo "scale=2;100*$Directory_Size/$Disk_Size+0.5" | bc | awk '{printf("%d\n",$1 + 0.5)}') done 

然后用15分钟的crontab条目调用它

 */15 * * * * sh /home/computer/limit-directory-size.sh /media/computer/mypartition/videos 88 120 

希望能帮助到你!

我开始考虑只保留一定数量的文件会有多难。 我转向awk,我已经有一段时间没用了,然后想出了下面的一个class轮。

 cd /path/to/Camera1 && ls -ltc | awk '{ if (!system("test -f " $9)) { size += $5; if (size > 5*2^30 ) system("rm " $9) } }' 
  1. 更改为相关目录
  2. 列表文件,最新的
  3. 在输出上运行awk,检查它是否是常规文件,将文件大小添加到计数器,如果累积大小超过5 gigs则删除文件

您可以将“rm”更改为“ls”以使其列出要删除的文件。 如果不仔细测试一个脚本是疯狂的,这个脚本是网上一个未知的,它会删除文件!

如果文件名中有滑稽的字符(例如空格),脚本可能会破坏和/或不执行您期望的操作。

awk的出色解决方案!

然而,我会采取额外措施来阻止你的垃圾桶溢出。

在我的cron ..

如果超过500mb擦拭旧凸轮镜头(请注意尺寸+ = 5美元的修正)

 */2 * * * * cd /home/me/Desktop/Dropbox/a_security_cam && ls -ltc | awk '{ if (!system("test -f " $9)) { size += $5; if (size > 0.5*2^30 ) system("rm " $9) } }' 

最先将垃圾清空至2gb以上

 */10 * * * * autotrash –min-free 2048 

见http://www.logfish.net/pr/autotrash/

你可以编写一个只有一个find命令的bash脚本

 find /Camera1 -atime +7 -exec rm {} \; 

这将从文件夹/ Camera1中删除7天内未访问过的所有文件。
显然这不是你想要的,但我希望它有所帮助。