Waiting till the directory becomes empty


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Waiting till the directory becomes empty
# 1  
Old 10-21-2010
Question Waiting till the directory becomes empty

Hi Guys,

I am writing a shell script to check whether the directory is empty or not.
If it is empty then proceed but if it not then wait till it becomes empty.

Is there any way for this?

Suggestions are welcome!!
Thanks in advance!Smilie
# 2  
Old 10-21-2010
Code:
 
ls -a | wc -l

If it is 2, then the directory is empty. If it is more than that then it is not empty.
# 3  
Old 10-21-2010
Thank you for the reply,
But at some time the directory is going to have some folders in it, and after some time it is going to be empty.
I want to wait till this directory gets empty.

ls -a | wc -l will give the status at that time.
I want to wait for that.

Something in while loop can help?
# 4  
Old 10-21-2010
elaborating one that, something like this?
Code:
while ls | grep -q .
do  
  sleep 2
done

What have you tried so far?

<br />---------- Post updated at 08:45 ---------- Previous update was at 08:39 ----------<br />

This is better I think:
Code:
until [ "$(echo *)" == "*" ]
do 
  sleep 2
done

# 5  
Old 10-21-2010
Im trying this,

FILE=""
DIR="./tmp"

while true
if [ "$(ls -A $DIR)" ]; then
sleep (10)
else exit(0);
done
# 6  
Old 10-21-2010
Code:
#!/bin/bash
while true
 do 
    if [ `find YOURFOLDER/|wc -l` -ne 1 ] ; then 
       echo -e "Not Processing -> [ waiting for empty ]\n File list ..\n`ls -l YOURFOLDER/|grep -v total`"; 
       echo "" ; sleep 10
    else 
       echo -e "Processing now ..\nProcess is now complete"; 
       break; 
    fi 
 done

# 7  
Old 10-21-2010
Correct syntax would be:
Code:
while true
do
  if [ "$(ls -A "$DIR")" ]; then
    sleep 10
  else 
    break
  fi
done

Which would be equivalent to:
Code:
while [ "$(ls -A "$DIR")" ]
do
  sleep 10
done

The reason this will not work is that ls -A does not give a usable return code to test on ($?).
So you could do this:
Code:
while [ "$(ls -A "$DIR")" != "" ]
do   
  sleep 10
done

or
Code:
until [ "$(ls -A "$DIR")" = "" ]
do   
  sleep 10
done



<br />---------- Post updated at 09:08 ---------- Previous update was at 09:06 ----------<br />

If you use this you do not need an external program (ls) which is a bit more efficient:
Code:
until [ "$(echo "$DIR/"*)" = "$DIR/*" ]
do 
  sleep 2
done


Last edited by Scrutinizer; 10-21-2010 at 04:24 AM..
This User Gave Thanks to Scrutinizer For This Post:
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. Shell Programming and Scripting

check empty directory !!!

I need to check if a directory is empty using an if condition in the pseudocode below if ; then else although i looked at a few forums on this topic, I left feeling a little unclear and i could not use the command successfully what can i substitute in the if conditon above,... (2 Replies)
Discussion started by: allah_waris45
2 Replies

3. 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

4. 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

5. 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

6. Shell Programming and Scripting

How to print lines till till a pattern is matched in loop

Dear All I have a file like this 112534554 446538656 444695656 225696966 226569744 228787874 113536566 443533535 222564552 115464656 225445345 225533234 I want to cut the file into different parts where the first two columns are '11' . The first two columns will be either... (3 Replies)
Discussion started by: anoopvraj
3 Replies

7. 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

8. Shell Programming and Scripting

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... (0 Replies)
Discussion started by: olimiles
0 Replies

9. Shell Programming and Scripting

need to read a file and keep waiting till it satisfies some condition

In my script i am writing to a counter file the no of processes i had started, that is each time i start a process, i will increment the content of counter file and also when the process ends i will decrement the content of the file. after this i do some other activities, by now i want to... (1 Reply)
Discussion started by: senthilk615
1 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