Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
share|improve this question
    
You can also use -mindepth 1 in find and skip the first if statement. And instead of ls and then reading $?, you can use the built-in [ -f $SPECIAL_FILE ] (see man 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 the find 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 the for statement as it stands, then you will only get a single iterator). –  Daniel Andersson Mar 30 '12 at 15:27

2 Answers 2

up vote 1 down vote accepted
find -maxdepth 1

only finds files of maximum path depth 1 under the argument directory.

Also: replace rm -rf with a simple echo when trying out changes, as you most probably do not want to find out the hard way that your changes are not what they are supposed to be.

-maxdepth 1 will make the script only look for the special file in the first sub directories, and if it is not found, delete them. If the special file resides deeper, you have to do a full depth check, but this solution fits my interpretation of your question in its original form.

share|improve this answer
    
Hi Daniel thanks for the input. Where exactly do I need to change "max depth 1"? sorry for being a nub :D –  JoyIan Yee-Hernandez Mar 30 '12 at 15:09
    
@JoyIanYee-Hernandez: Try for d in $(find $FIND_BASE -maxdepth 1 -type d -print); do. –  Daniel Andersson Mar 30 '12 at 15:10
    
Thank you @Daniel Andersson That worked like a charm :) thanks again –  JoyIan Yee-Hernandez Mar 30 '12 at 15:14

Since you're using bash, you don't even need find:

shopt -s globstar nullglob

for subdir in "$FIND_BASE"/*/     # the trailing slash picks out just the subdirectories  
do
    # find special files anywhere under the subdir
    flag_files=( "$subdir"/**/DONOTDELETE.{txt,TXT} )  

    if [[ ${#flag_files[*]} -ne 0 ]]; then
        echo "Ignoring $subdir, contains ${flag_files[*]}"
    else
        echo rm -r "$subdir"
    fi

done
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.