The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Script to remove all empty files within the directory structure? cat123 Shell Programming and Scripting 5 06-05-2008 06:01 AM
remove empty directory sirrtuan Shell Programming and Scripting 6 03-04-2008 06:50 AM
deleting empty files in a directory berlin_germany Shell Programming and Scripting 5 01-26-2007 12:47 PM
How to ufsrestore to a different machine, empty directory joekerr SUN Solaris 1 07-22-2005 07:46 AM
rmdir a non-empty directory. yls177 UNIX for Dummies Questions & Answers 2 12-11-2002 04:44 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-31-2006
Registered User
 

Join Date: Aug 2006
Posts: 18
Setting the right directory as non-empty

Hi guys,

I've been trying to get this part of my script to work for ages and now it's getting me annoyed!! So i thought a fresh group of eyes could see where i'm going wrong!

The idea is getting a directory and all its files moved to a temp folder, but similar to the rm -ir command where it asks if i want certain files/direcories moved etc.

Now the case i have is this: i've already said no to a file i don't want moved. Then when the process comes to the directory that this file is in and asks to move the directory, if i say yes then it'll tell me that i can't as it's non-empty. Then continues with the rest of the task.

I've got it working to a degree, but it takes the first non-empty directory, and any child directory is automatically set as non-empty too, even if they're not. Ideally it should set the lowest non-empty child directory, as then all the parent directories would automatically be set as non-empty too.

Here's the code i'm stuck with anyway:

Code:
function directoryDelete () {

if [[ -d "$1"  &&  "$FLAG_R" == "R" && "$FLAG_F_I" == "i" ]] ; then
   echo -n "saferm: descend into directory \`$1'?"
   read ANSWER
   if [[ "$ANSWER" = [Yy] ]] ; then
      echo  "removing all entries in directory \`$1'"
      for FILE in $1/*
      do
         if [ -d $FILE ] ; then
            directoryDelete $FILE
         else
            fileDelete $FILE
         fi
      done
      if [ "$DIR_NOT_EMPTY" = "y" ] ; then
         echo -n "saferm: remove directory \`$1' (might be nonempty)?"
         read ANSWER
         if [[ "$ANSWER" = [Yy] ]] ; then
            echo "saferm: cannot remove directory \`$1': Directory non-empty"
         fi
      else
         echo -n "saferm: remove directory \`$1'?"
         read ANSWER
         if [[ "$ANSWER" = [Yy] ]] ; then
            mv  $1 $TRASH 2>/dev/null
            echo "saferm: removing directory itself: \`$1'"
         fi
      fi
   fi
else
   fileDelete $1
fi
}


function fileDelete() {

if [ "$FLAG_F_I" = "i" ] &&  [ -w  "$1" ] ; then
   interactive $1
elif [ "$FLAG_F_I" = "f"  ] ; then
   force $1
elif [ "$FLAG_R" = "R"  ] ; then
   remove $1
else
   remove $1
fi
}

function interactive () {
echo -n "saferm: remove $1 ?"
read ANSWER
if [[ "$ANSWER" = [Yy] ]] ; then
   remove $1
else
   DIR_NOT_EMPTY=y
fi
}

Apologies if it's not clear, it's the interactive bit i'm stuck on. Any help would get this done and let my brain rest!!

Many thanks in advance

Oliver
Reply With Quote
Google The UNIX and Linux Forums
Forum Sponsor
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 11:22 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0