#!/bin/bash # 05/26/11 # dayid@dayid.org # Cleans out .trash file whenever disk space is less than 5% available for /home # This is because I use my "del" command rather than RM. # Note: my ~/.trash is a symlink to ~/.local/share/Trash/files since I use gnome # This should be run in a crontab as necessary. # # Default to allow up to 95% of /home to be used... allow override with a variable if [ $1 ];then CLEANOUT=$1; else CLEANOUT=95; fi date > ~/.cleantrash echo "Proceeding with cleanout ${CLEANOUT}%" | tee -a ~/.cleantrash cd ~/.trash/ # The following line may need to be modified pending how your df output is. # The below line works for my regular-fs system, but for my # Fedora Core system with LVM, I need to use '{ print $4 }' # You'll also need to change the other 2 (total of 3) occurrences of '{ print $5 }' CHECK=$( df -h /home | tail -n 1 | awk '{ print $5 }' | cut -d "%" -f "1" ) CHECK1=$CHECK echo "Disk usage is ${CHECK}%" | tee -a ~/.cleantrash 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 CHECK=$( df -h /home | tail -n 1 | awk '{ print $5 }' | cut -d "%" -f "1" ) done CHECK2=$( df -h /home | tail -n 1 | awk '{ print $5 }' | cut -d "%" -f "1" ) 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