user270488
user270488

Reputation: 59

Folder/subfolder size then deletion

I am new in the world of Linux:please, accept my apology for this question:

I have written a script which check the size of a folder and ALL of its subfolder and if the size is greater than X bytes then files which have been modified more than X days ago will be deleted.

I have put folder size zero for test purpose, therefore, the script should have perform the deletion process, but, it does NOT.

This script does do what I expect and I do not know why.

Thanks for your help.
Script is:

#!/bin/sh

# 100GB SIZE LIMIT
SIZE="0"

# check the current size
CHECK="`du /media/nssvolumes/TEST/MB/`"

if ["$CHECK" -gt "$SIZE"]; then echo "$ACTION"
ACTION="`find /media/nssvolumes/TEST/MB/ -mindepth 0 -maxdepth 3 -mtime +1 -type f  -exec rm -f {} \;`"
else exit
fi

Upvotes: 1

Views: 289

Answers (1)

LiMar
LiMar

Reputation: 2972

Well, here we have a bunch of errors. Supposing your OS is Linux, try running du from the command line to realise what it returns, because it is NOT a number.

  • Second problem - using of [] requires spaces.
  • Also, integers does not require ""
  • You're printing $ACTION before its value is defined.
  • exit is not needed to be called if script has no more code to execute

So the fixed script is:

#!/bin/sh

# 100GB SIZE LIMIT
SIZE=0
MY_DIR="/media/nssvolumes/TEST/MB/"

# check the current size
CHECK=$(du -bs $MY_DIR|awk '{print $1}')

if [ $CHECK -gt $SIZE ]; then
    echo "ACTION"
    find $MY_DIR -mindepth 0 -maxdepth 3 -mtime +1 -type f -exec rm {} \;    
fi

Upvotes: 1

Related Questions