#!/bin/bash # 05/26/11 # 08/07/11: added quota support # dayid@dayid.org # if [ $1 ]; then case $1 in -d) # Allow override of 95% of REAL DISK usage if [ $2 ]; then CLEANOUT=$2; else echo "You must specify a number value here from 1-100" && exit 1; fi TYPE=disk CHECK=$( df -h /home 2>/dev/null | tail -n 1 | awk '{ print $5 }' | cut -d "%" -f "1" ) ;; -q) # Allow override of REAL DISK by using percent of QUOTA instead if [ $2 ]; then CLEANOUT=$2; else echo "You must specify a number value here from 1-100" && exit 1; fi TYPE=quota CHECK=$( quota -f /home 2>/dev/null | tail -n 1 | awk '{ print $2" / "$3" * 100" }' | bc -ql | cut -d "." -f 1 ) ;; esac else # Default to allow up to 95% of /home to be used. CLEANOUT=95 type=disk CHECK=$USAGED fi CHECK1=$CHECK date > ~/.cleantrash echo "Current usage is ${CHECK}% of $TYPE." | tee -a ~/.cleantrash echo "Proceeding cleanout to match ${CLEANOUT}% or less of ${TYPE}." | tee -a ~/.cleantrash cd ~/.trash/ if [ $CHECK -gt $CLEANOUT ] then echo "Since ${CHECK}% is > ${CLEANOUT}%, it's time to clean." | tee -a ~/.cleantrash ls -a | egrep "[\'\"\(\)\&\#\$\^\!\ ]" > ~/.namestrash while read FILE; do NEW=`echo ${FILE} | tr "[\'\"\(\)\&\#\$\^\!\ ]" '_'` mv -f "${FILE}" "${NEW}" && echo "Renamed: ${FILE} to ${NEW}" done < ~/.namestrash find . -size 0 -exec /bin/rm -fv {} \; | tee -a ~/.cleantrash while [ $CHECK -gt $CLEANOUT ]; do RMFILE=`ls -trc | head -2 | tail -1` if [ $RMFILE ]; then true else echo "Trash is empty but /home is still ${CHECK}% full and you wanted ${CLEANOUT}%" | tee -a ~/.cleantrash cat ~/.cleantrash | mail -s "cleantrash.sh: Can't clear more space" dayid && echo "Mailing you" exit 1 fi /bin/rm -rfv -- "${RMFILE}" | tee -a ~/.cleantrash case $TYPE in disk) CHECK=$( df -h /home | tail -n 1 | awk '{ print $5 }' | cut -d "%" -f "1" ) ;; quota) CHECK=$( quota -f /home 2>/dev/null | tail -n 1 | awk '{ print $2" / "$3" * 100" }' | bc -ql | cut -d "." -f 1 ) ;; esac done case $TYPE in disk) CHECK2=$( df -h /home | tail -n 1 | awk '{ print $5 }' | cut -d "%" -f "1" ) ;; quota) CHECK2=$( quota -f /home 2>/dev/null | tail -n 1 | awk '{ print $2" / "$3" * 100" }' | bc -ql | cut -d "." -f 1 ) ;; esac echo "Disk usage is now ${CHECK2}% of allowed ${CLEANOUT}%. It was ${CHECK1}% when we started." | tee -a ~/.cleantrash cat ~/.cleantrash | mail -s "cleantrash.sh: Had to remove files in .trash to save space" dayid && echo "Mailing you a list of removed files" else echo "${CHECK}% is <= ${CLEANOUT}%, exiting"; exit 0 fi