1

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
derobert
  • 109,670
Jasper
  • 43

2 Answers2

4

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
  • 10,793
  • 1
    you could simplify that by changing | while read line to | while read bytes filename and removing the echo | cut calculations. – Tim Kennedy Aug 06 '12 at 16:38
  • 1
    How that satisfies the “this need to be the oldest files/folders” part of the requirement? – manatwork Aug 06 '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 06 '12 at 16:59
  • anyone that can help after my edit? – Jasper Aug 07 '12 at 09:51
  • @Jasper help with what exactly? you just pasted some random code without any question – scai Aug 07 '12 at 10:44
  • That is because the reply was deleted beneath this answer where i explain it. this was because i may not answer to my own question.

    Explanation: Scai told me this script aint possible with SAMBA shares. I need an adjusted script that can work on my Synology DS-409 with SAMBA shares support. The code in my edited post is a example of what is maybe working.

    – Jasper Aug 07 '12 at 11:12
  • This also cannot work on a remote share except you mount it locally or execute the script remote. – scai Aug 07 '12 at 11:16
  • I will upload the script locally on the NAS itself, but the NAS works with SAMBA file shares. That's wy the code from scai aint working. – Jasper Aug 07 '12 at 11:29
  • You seem to mix up things. If you run it on the NAS itself it doesn't matter if the local folders are exported as sambas shares or not. – scai Aug 07 '12 at 12:23
  • Is it possible to explain this to me so i understand? And is there a possibility to put this script on my NAS and run this with SAMBA shares? If yes, where do i need to ask such question? – Jasper Aug 07 '12 at 12:58
  • What exactly prevents you from running it on your NAS? This isn't the right place for such discussions, choose a forum or mailing list instead. – scai Aug 07 '12 at 13:01
  • I don't test this so i don't lose important data from my NAS. – Jasper Aug 07 '12 at 13:27
  • So you will never run any script for your task? Test it on unimportant data. – scai Aug 07 '12 at 13:29
  • Good idea, i will do that this evening and report back to you. – Jasper Aug 07 '12 at 13:31
  • Ok tryed this on a linux machine trough SSH but getting following errors: code: not foundtxt: line 4: autodelete.txt: line 11: syntax error: unexpected "done" (expecting "fi") After deleting line 4 and line 11 done i get following error: code autodelete.txt: line 10: syntax error: unexpected end of file (expecting "do") So annymore help is apriciated. Thanks. – Jasper Aug 21 '12 at 17:18
  • There is no autodelete.txt in the script I posted. – scai Aug 21 '12 at 17:31
  • scai, autodelete.txt is a file i created in windows and uploaded to a linux share :) – Jasper Aug 21 '12 at 18:24
  • There is still way too less information about how you run the script. – scai Aug 21 '12 at 18:25
  • I have linux, in nano tekst editor i created a file from your script, Named "autodelete.txt", DIRS="/storage/movies/", stored in "/storage/.scripts/" I have created a Cronjob to run the script every saterday. But now i start it manually to test in Putty (SSH) with the command "sh autodelete.txt" Need more info? tell me what you need. – Jasper Aug 21 '12 at 18:57
0

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
Jasper
  • 43
  • 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 06:33
  • where can i find a forum that can help me? – Jasper Aug 23 '12 at 06: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 07: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