I got a script running and its cool and all. Any idea how to make this script run under just 1 level of subdirectory?
#! /bin/bash
#This will delete the sub-directories of a specific directory when it does not contain a DONOTDELETE.txt or DONOTDELETE.TXT File.
SPECIAL_FILE=DONOTDELETE*
LOGFILE=clean.$(date +"%Y-%d-%m.%H%M%S").log
FIND_BASE="$1"
if [ $# -ne 1 ]; then
echo "Syntax $(basename $0) <FIND_BASE>"
exit 1
fi
if [ "$FIND_BASE" = "." ]; then
FIND_BASE=$(pwd)
else
FIND_BASE=$(pwd)/$FIND_BASE
fi
for d in $(find $FIND_BASE -type d -print); do
if [ "$d" != "$FIND_BASE" ]; then
ls $d | grep $SPECIAL_FILE &> /dev/null
if [ $? -ne 0 ]; then
echo "Deleting $d" | tee -a $LOGFILE
rm -rf $d 2>/dev/null
else
echo "Ignoring $d, contains $SPECIAL_FILE" | tee -a $LOGFILE
fi
fi
done
exit 0
-mindepth 1
infind
and skip the firstif
statement. And instead ofls
and then reading$?
, you can use the built-in[ -f $SPECIAL_FILE ]
(seeman test
). You will also have issues with directories with spaces as it stands; quote at least the third FIND_BASE assignment and the$FIND_BASE
in thefind
command. Quoting is always good. Until you know exactly when it's not needed: use it everywhere it is possible; every assignment, every variable call (not in thefor
statement as it stands, then you will only get a single iterator).