Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I'm looking for a bash script that would auto-delete oldest folders (lets say last 3) in a directory when the available space drops below X gigs.

Any ideas?

*Ive red related posts but since I don't know anything, I cant modify the scripts myself.

Thanks, Halik

share|improve this question
    
Include the links to the related posts. I will try to modify them for you. Just be very specific about which directories you want to work on. –  Mat Banik Jan 5 '11 at 0:08
    
Just want to note that finding the "oldest folder" can be tricky. If you create /foo/bar/baz.txt, then bar's modification time will be updated, but foo's will not. Likewise, if you modify /foo/bar/baz.txt in place, not even bar's time will be updated because no directory entries were changed. If this needs to be taken into account, let us know. –  Jander Jan 5 '11 at 6:28
add comment

1 Answer

up vote 2 down vote accepted

You need to tailor the PARTITION, DIR_TO_CHECK, LIMIT and NUM_DIRS_TO_DELETE, BLOCKSIZE and NUM_DIRS_TO_DELETE to suit you needs.

The LIMIT is the number of gigabytes available.

BE CAREFUL WITH THIS, once you uncomment the rm command, the directories it deletes are gone!

#!/bin/bash
IFS=$'\n'

PARTITION="/dev/sdb1"
DIR_TO_CHECK="/home/bryan/temp/*/"
BLOCKSIZE=1000000000 
LIMIT=12
NUM_DIRS_TO_DELETE=4


if [ $(df  -B $BLOCKSIZE  $PARTITION | tail -1 |  awk  '{ print $4}') -gt $LIMIT ]; then
  echo "plenty of space"
else 
   for i in $(ls -d $DIR_TO_CHECK --group-directories-first  -tr | head -$NUM_DIRS_TO_DELETE)
   do

      du -h $i
      # When you are happy that the script does what you want, 
      # delete the above line and remove the # character from the below
      # rm -r $i 
   done
fi
share|improve this answer
1  
There are two ways this script will fail. The biggest is that you can't rely on -f18 being the correct field number because the number of spaces will vary depending on the length of the number strings. It would be better to use AWK to pick the field. Also, if there are spaces in filenames or directory names, the for loop will fail. ls -d /foo/*/ will only list directories because of the final slash. df -k lists sizes based on 1K blocks, but it's unclear whether your LIMIT variable is in bytes or K-bytes. You should use $() for command substitution instead of backticks. –  Dennis Williamson Jan 5 '11 at 1:35
    
Thanks for the suggestions, have made the changes. Please take another look at it and let me know what you think. –  bryan Jan 5 '11 at 2:10
    
A silly question: will the script check constantly for the if condition or only on execution? And if only on execution, how to make it execute in some time intervals or on new folder creation? –  Halik Jan 5 '11 at 17:06
    
It will only execute when called. If you want it to run a regular intervals use cron - clickmojo.com/code/cron-tutorial.html. –  bryan Jan 5 '11 at 17:09
    
Roger that, thanks! –  Halik Jan 5 '11 at 19:21
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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