Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!pacific.mps.ohio-state.edu!linac!att!cbnews!ask From: ask@cbnews.att.com (Arthur S. Kamlet) Newsgroups: comp.unix.questions Subject: Re: How do I tell when a directory is empty in a script? Message-ID: <1991Apr3.225433.22337@cbnews.att.com> Date: 3 Apr 91 22:54:33 GMT References: <563@bria> <1991Apr3.211052.893@cbnewsl.att.com> Reply-To: ask@cblph.att.com Distribution: na Organization: AT&T Bell Laboratories, Columbus, Ohio Lines: 23 In article <1991Apr3.211052.893@cbnewsl.att.com> urban@cbnewsl.att.com (john.urban) writes: >>>When I am running a shell script, how can I tell when a directory is empty? > >Why not just run rmdir $directory and if it works, then the directory was empty. >if [ -d test1 ] >then rmdir test1 > if [ -d test1 ] > then echo Not empty directory test1 > else mkdir test1 > echo Empty directory test1 > fi >fi If the reason for knowing if a directory is empty is to know if it can be removed, then rmdir test1 is sufficient. Note: The above code will create a new test1 directory which could be smaller than the original test1 directory. All of these scripts, of course, assume the directory and the parent directory have correct read/write/search permissions for the user. -- Art Kamlet a_s_kamlet@att.com AT&T Bell Laboratories, Columbus