rmdir () { ################################################################################ # A version of rmdir which will remove .DS_Store files IFF that is the only file # in an otherwise empty directory # # Intended to be used a shell function (zsh or compatible) # # Created by TJ Luoma on 2009-12-15 # Tested on Mac OS X 10.6.2 using Zsh 4.3.9 # Contact: http://luo.ma # # Free to use/share/modify. I'd be grateful to receive a copy of # any corrections/improvements that you make. # # Use at your own risk. # use a temp file TMPFILE=/tmp/rmdir.$USER.$$ # make sure the temp file isn't already in use rm -f $TMPFILE # See 'man rmdir' for explanation of -p" # if it is there, we pass it along # if not, we ignore it if [ "$1" = "-p" ] then P="-p" shift else P="" fi for DIR in $@ do if [ -d "$DIR" ] then # this is where rmdir is for Mac OS X 10.6 /bin/rmdir $P "$DIR" 2>$TMPFILE && echo "rmdir: removed $DIR" RMDIR_EXIT="$?" if [ "$RMDIR_EXIT" = "1" ] then # /bin/rmdir will exit = 1 IF # either the directory is NOT empty # or not found # BUT we have already checked to make # sure that it exists # so we can ASSUME this means it # is not empty # (although there may be other cases?) FILES_IN_DIR=`find "$DIR" -print -maxdepth 1 |wc -l|awk '{print $1}'` if [ "$FILES_IN_DIR" = "2" ] then # NOTE: The directory will ALWAYS be returned, so that's 1 # if there is ONE other file/directory inside that directory # then it will be equal to 2 # IFF there is only one file in the Directory, we check to see if that file is .DS_Store # If it is, we delete it and then run rmdir if [ -f "$DIR/.DS_Store" ] then # If we find a file .DS_Store in that directory # delete it # and then delete the directory # and then tell me you did it rm -f "${DIR}/.DS_Store" && /bin/rmdir $P "$DIR" && echo "rmdir: removed $DIR" else # If we get here, there was only one file in the directory # BUT that file was NOT .DS_Store echo "rmdir: $DIR: Directory not empty" fi else # If we get here, there is more than 1 file in the directory # Show whatever error we received before cat $TMPFILE fi # if/else found only 2 files using 'find' else # We received an error, but that error was NOT = 1 # No idea if this could ever happen cat $TMPFILE fi # if/else rmdir exit = 1 else # current argument is NOT a directory/folder echo "rmdir: $DIR is not found, or is not a directory" fi # if/else a directory done # loop for arguments given # clean up our temp file rm -f $TMPFILE }