I have a big share (~5TB) that is getting full. Now I want to make a script that deletes data from 2 specified folders. But this need to be the oldest files/folders and it need to stop when ~50GB has been removed, so it won't delete all folders.
Edit: This need to work with Samba shares for my Synology DS-409. The script need to run on the Synology in /etc/crontab.
Somewhere else they gave me this code:
#!/opt/bin/bash
dir=/data/video
min_dirs=3
full=60
logfile=/var/tmp/removed.log
df=`df | grep data | awk '{print $5}' | sed s/%//g`
if [ $df -gt $full ]; then
[[ $(find "$dir" -type d | wc -l) -ge $min_dirs ]] &&
IFS= read -r -d $'\0' line < <(find "$dir" -printf '%T@ %p\0' 2>/dev/null | sort -z -n)
file="${line#* }"
ls -lLd "$file"
#rm -rf "$file"
date=`date`
if [ -f "$file" ]; then
echo "$date $file could not be removed!" >> $logfile
else
echo "$date $file removed" >> $logfile
fi
fi