#!/usr/bin/env bash # Author: Oliver Ladner # License: LGPL # # Cleans directories from garbage files # Where to look DIR="/mnt/dir1 /mnt/dir2" # Garbage files (do NOT add desktop.ini/thumbs.db) # Example to delete every WMV-file: #HATED_FILES=' -o -iname '*.wmv' ' HATED_FILES=' -o -iname '.DS_Store' -o -iname '*.url' ' # Locate dirs/files with setUID #echo -n "Looking for setUID dirs in $DIR..." #COUNT_SUID=`find $DIR -type d -perm -u=s -print | wc -l` # #if [ "$COUNT_SUID" -eq 0 ]; then # echo " OK, no setUID directories found" #else # echo " Found $COUNT_SUID directories" # echo -n "Doing 'chmod -R u-s'..." # # Remove the setUID bit # find $DIR -type d -perm -u=s -exec chmod -R u-s {} \; # if [ ! "$?" -eq 0 ]; then # echo " ERROR while chmodding" # exit 1 # else # echo " OK" # fi #fi # FIXME: add search for dirs/files with weird names #find $DIR -maxdepth 100 -type d -print | grep '?' # Get rid of files like Thumbs.db and desktop.ini, echo -n "Searching garbage files..." find $DIR \( -iname 'desktop.ini' -o -iname 'thumbs.db' $HATED_FILES \) -print > /tmp/cleanup.$$ if [ $? -gt 0 ]; then exit 1 fi if [ $(grep -v -c '^$' /tmp/cleanup.$$) -eq 0 ]; then echo " OK, no garbage files found" else echo " Found $(grep -v -c '^$' /tmp/cleanup.$$) garbage files" echo -n "Doing 'rm -f'..." # Search again and delete the crap cat /tmp/cleanup.$$ | while read LINE; do rm "$LINE" if [ ! "$?" -eq 0 ]; then echo " ERROR while deleting garbage file $i" RC=1 else RC=0 fi done fi rm /tmp/cleanup.$$ exit $RC