Help on simple shell script

Hello,

I am running openmediavault on my Raspberry and I would like to use it as a backup FTP server of snapshots taken from my IP cams.

So I get the network recorder to upload every 3 seconds a snapshot to the Raspberry. Everything works perfectly.

I would need now a simple script that checks the size of a directory (Backup_NVR) and if it exceeds for instance 3 GB, it deletes recursively all images and empty directories older than 60 minutes.

I would schedule this script as a job and it should be done.

Being new to shell scripting, although I have IT experience, can anyone please draft this script for me?

I would be very grateful, thanks a lot, Daniele

The command for directory usage - du -

 du -s --block-size G  /fullpath/to/Backup_NVR

Gives the size in GB.

# get size of directory
sz=$(du -s --block-size G  /fullpath/to/Backup_NVR | awk '{print int($0)}')
if [ $sz -ge 3 ] ; then  # too big delete older files
   find /path/to/Backup_NVR -type f -mmin +60 -exec rm {} \;
fi

---------- Post updated at 12:38 AM ---------- Previous update was at 12:38 AM ----------

Thank you so much, I will try and advise!

---------- Post updated at 12:40 AM ---------- Previous update was at 12:38 AM ----------

Hello,
Again thanks for the script.

Does it also delete the empty directories or shall I add anything else?
Please advise, daniele

-delete is faster than -exec rm .
After deleting the files run another find that deletes the empty directories

find /path/to/Backup_NVR -type f -mmin +60 -delete
find /path/to/Backup_NVR -mindepth 1 -depth -type d -empty -delete

Thanks everybody for helping.
I have written the entire script, inserting a pause to check the directory size before delete and to debug it.

Can anyone please check if it is correct?
Thanks.

QUOTE

#!/bin/bash
# delete files oder than MM minutes if the size exceeds GG gb

MM=60
GG=3

# get size of directory
sz=$(du -s --block-size G  ./Backup_NVR | awk '{print int($0)}')

echo �Directory size: � $sz
read -p "Press [Enter] key to continue��

if [ $sz -ge $GG ] ; then  # too big delete older files

       find ./Backup_NVR -type f -mmin +$MM -delete
    find ./Backup_NVR -mindepth 1 -depth -type d -empty -delete
fi

UNQUOTE

I don't see anything obviously wrong You have edited this in Microsoft Word which has inserted a few nasty "smart quotes" into it. Replace all smart quotes with ' and " in an actual text editor.

And definitely do a "dry run" first: Print filenames instead of deleting them.

I'm not sure how the pause helps you; once you hit it, it either deletes the right files or the wrong ones...

#!/bin/bash
# delete files oder than MM minutes if the size exceeds GG gb

MM=60
GG=3

# get size of directory
sz=$(du -s --block-size G  ./Backup_NVR | awk '{print int($0)}')

echo "Directory size: " $sz
read -p "Press [Enter] key to continue��

if [ $sz -ge $GG ] ; then  # too big delete older files

       find ./Backup_NVR -type f -mmin +$MM -print
    find ./Backup_NVR -mindepth 1 -depth -type d -empty -print
fi