OpenWrt Forum Archive

Topic: script for auto deleting file

The content of this topic has been archived on 26 Apr 2018. There are no obvious gaps in this topic, but there may still be some posts missing at the end.

hello all
i have a router with OpenWrt Chaos Calmer 15.05
i connect it with a dlink ipcam DCS 920 ,and enable ftp in camera dlink ,to put recorded images in directory / usb storage that connected to openwrt router,
but i look for a script erasing all recorded pictures when directory is full weekly or monthly, or script that manage capacity to avoid router storage.

logrotate may work for you to keep from filling things up.  As the name implies it was designed for log files but it works against any files that you want and can compress or run shell scripts basically tons of optoins.

Aside from that you can write your own script and place it in cron.  could be a shell script or perl or whatever you desire.

create an executable (chmod +x check.sh) check.sh in /root and then put it in cron, for example check every day at 6am:

0 6 * * * /root/check.sh &

#!/bin/sh

DIR=/mnt/share/pictures
MAXMB=1000

if [[ -d $DIR ]]; then
  SIZE=$(du -sm $DIR | cut -f1)
  
  if [[ $SIZE -gt $MAXMB ]]; then
    rm -r $DIR/*
  fi
fi

exit 0

will delete all files in /mnt/share/pictures if size of this dir is greater than 1000mb.

(Last edited by makedir on 13 Dec 2017, 07:37)

You can use find to delete everything older than e.g. 31 days:
find /path/to/usb -mtime +31 -delete

The discussion might have continued from here.