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:


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"
   if [ -f "$file" ]; then
      echo "$date $file could not be removed!" >> $logfile
      echo "$date $file removed" >> $logfile

This should work:

DIRS="a/ b/"
MAXDELBYTES="53687091200" # 50GB

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

    if [ $DELBYTES -ge $MAXDELBYTES ]; then break; fi
  • 1
    you could simplify that by changing | while read line to | while read bytes filename and removing the echo | cut calculations. – Tim Kennedy Aug 6 '12 at 16:38
  • 1
    How that satisfies the “this need to be the oldest files/folders” part of the requirement? – manatwork Aug 6 '12 at 16:46
  • @manatwork it didn't, I forgot to include the modification time. Updated it and now it should work as expected. Thanks! – scai Aug 6 '12 at 16:59
  • anyone that can help after my edit? – Jasper Aug 7 '12 at 9:51
  • @Jasper help with what exactly? you just pasted some random code without any question – scai Aug 7 '12 at 10:44

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

        ! 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
  • In the script I posted there is no bytes in line 11. Seems like you modified the script. And this isn't the right place for such a discussion, better use some forum and hot a help site. – scai Aug 22 '12 at 6:33
  • where can i find a forum that can help me? – Jasper Aug 23 '12 at 6:21
  • ok found out wy it aint working. now i posted a new question so this one does not get spammed. link – Jasper Aug 23 '12 at 7:23
  • You seem to have a non-GNU version of find which has no -printf option, so it won't work that way on your system. – scai Aug 23 '12 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.