Bash script to keep folder at a certain size by deleting oldest files.

A place for discussion of topics that are not specific to ZoneMinder. This could include Linux, Video4Linux, CCTV cameras or any other topic.
Post Reply
BaconButty
Posts: 35
Joined: Fri Jun 12, 2020 1:29 pm

Bash script to keep folder at a certain size by deleting oldest files.

Post by BaconButty »

I Finally got this working as I wanted. I want to keep my Zoneminder events folder below 10gb, and this can't really be done with zoneminder unless you start playing around with creating virtual drives.

So here's a simple bash script that's mostly taken from stackoverflow that I''ve proven works flawlessly. The two zmaudit.pl are intentional, for some reason it needs to be called twice to actually cleanup the folder.

Code: Select all


#!/bin/bash
usage=$(du -sb /var/cache/zoneminder/HighResCamEvents  | cut -d $'\t' -f 1)
max=10000000000
if (( usage > max ))
then
  find /var/cache/zoneminder/HighResCamEvents -maxdepth 5 -type f -printf '%T@\t%s\t%p\n' | sort -n | \
    while (( usage > max )) && IFS=$'\t' read timestamp size file
    do
      rm -- "$file" && (( usage -= size ))
    done
fi
zmaudit.pl
zmaudit.pl


Thought someone else might find it useful :)
Post Reply