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

```
['https://unix.stackexchange.com/questions/44871', 'https://unix.stackexchange.com', 'https://unix.stackexchange.com/users/21697/']
This should work:

```
DIRS="a/ b/"
MAXDELBYTES="53687091200" # 50GB
DELBYTES="0"

find $DIRS -type f -printf "%T@ %s %p\n" | sort -r -n | while read time bytes filename
do
    rm -fv "$filename"
    DELBYTES=$((DELBYTES + bytes))

    if [ $DELBYTES -ge $MAXDELBYTES ]; then break; fi
done

```
scai, autodelete.txt is a file i created in windows and uploaded to a linux share :)
Now i have made this code in nano because of windows linux code problems.

But now it gives a bunch of error's

```
root ~/.config # sh autodelete
find: unrecognized: -printf
BusyBox v1.20.2 (2012-08-09 05:49:15 CEST) multi-call binary.

Usage: find [PATH]... [OPTIONS] [ACTIONS]

Search for files and perform actions on them.
First failed action stops processing of current file.
Defaults: PATH is current directory, action is '-print'

        -follow         Follow symlinks

Actions:
        ! ACT           Invert ACT's success/failure
        ACT1 [-a] ACT2  If ACT1 fails, stop, else do ACT2
        ACT1 -o ACT2    If ACT1 succeeds, stop, else do ACT2
                        Note: -a has higher priority than -o
        -name PATTERN   Match file name (w/o directory name) to PATTERN
        -iname PATTERN  Case insensitive -name
        -path PATTERN   Match path to PATTERN
        -ipath PATTERN  Case insensitive -path
        -type X         File type is X (one of: f,d,l,b,c,...)
        -links N        Number of links is greater than (+N), less than (-N),
                        or exactly N
If none of the following actions is specified, -print is assumed
        -print          Print file name
        -exec CMD ARG ; Run CMD with all instances of {} replaced by
                        file name. Fails if CMD exits with nonzero

autodelete: line 11: bytes: not found

```