Setting the right directory as non-empty


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Setting the right directory as non-empty
# 1  
Old 08-31-2006
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
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl - How to empty a directory?

Hi Guys, i'm writing a perl script which whenever runs, should empty 3 pre-decided directories as first step and then the script has the logic to parse some other directories and copy the files inside those directories into these 3 directories. I've the logic already developed, just need to... (2 Replies)
Discussion started by: jhamaks
2 Replies

2. Homework & Coursework Questions

Check whether a Directory is empty or not

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: 1.pls tell me the command for checking whether a given directory is empty or not . 2. can i check what is the... (1 Reply)
Discussion started by: upvan111
1 Replies

3. Shell Programming and Scripting

Check whether a Directory is empty or not

1.pls tell me the command for checking whether a given directory is empty or not . 2. can i check what is the last copied item in a directory . and i yes , i want to move that last copied item in another directory . pls help me with shell code for these two tasks thanks (1 Reply)
Discussion started by: upvan111
1 Replies

4. SCO

Deleting a directory that is not empty

I know someone will probably laugh at this question, I probably knew the answer many years ago when I was doing this full time but here goes..... I have a directory that has many files and sub-directories in it, RMDIR will not delete a directory that is not empty so what is the command to... (1 Reply)
Discussion started by: moondogi
1 Replies

5. Shell Programming and Scripting

Empty Directory Check

Hi All, I have a requirement to check all the files in a directory and mail non empty files Files are in csv format , i need to skip header while checking pls help Thanks (12 Replies)
Discussion started by: gwrm
12 Replies

6. Shell Programming and Scripting

check whether the directory is empty or not

I have the list of users in user.log, under each user folder there is sub1 folder is there. i want to check whether sub1 is empty or not, if it is empty i have to skip that user user folder and iterate next user folders. i have the sample code,its not giving not proper results. while read line... (8 Replies)
Discussion started by: KiranKumarKarre
8 Replies

7. Shell Programming and Scripting

remove empty directory

Hi, I need to delete an empty directory in a temp directory except "dir5" (keep everything that is not empty). Plese advise. Here is an example of my directory. /dir/temp/ dir1 - delete if this is empty dir2 - delete if this is empty dir3 - delete if this is empty dir4 - delete if this... (7 Replies)
Discussion started by: sirrtuan
7 Replies

8. Shell Programming and Scripting

How do I tell if a directory is empty?

To see if a directory is has anything in it, I do this: if ; then # do something fi But surely there is a more easy-to-read and elegant way. Isn't there? (6 Replies)
Discussion started by: KenJackson
6 Replies

9. Shell Programming and Scripting

How to empty all files in a directory

Hi all, Can you tell me how to empty all files in a directory with a "find" command? It does not seem to work the way I try it: # ls -l *.dat -rw-r--r-- 1 root root 7 Jul 20 20:51 la2.dat -rw-r--r-- 1 root root 4 Jul 20 20:51 la.dat # find... (9 Replies)
Discussion started by: majormark
9 Replies

10. UNIX for Dummies Questions & Answers

rmdir a non-empty directory.

hi, i understand that rmdir will only remove direcotry when it is empty but are there options which will also remove non-empty directories? i went to man rmdir but only find the option -p? i am on solaris. thanks (2 Replies)
Discussion started by: yls177
2 Replies
Login or Register to Ask a Question