Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

LOGFILE=clean.$(date +"%Y-%d-%m.%H%M%S").log
if [ $# -ne 1 ]; then
        echo "Syntax $(basename $0) <FIND_BASE>"
        exit 1
if [ "$FIND_BASE" = "." ]; then

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
                        echo "Ignoring $d, contains $SPECIAL_FILE" | tee -a $LOGFILE
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
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  
    # 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[*]}"
        echo rm -r "$subdir"

share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .