How do I tell when a directory is empty in a script?

Arthur S. Kamlet ask at cbnews.att.com
Thu Apr 4 08:54:33 AEST 1991


In article <1991Apr3.211052.893 at cbnewsl.att.com> urban at 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 at att.com  AT&T Bell Laboratories, Columbus



More information about the Comp.unix.questions mailing list