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