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

john.urban urban at cbnewsl.att.com
Thu Apr 4 07:10:52 AEST 1991


>In an article, mml0.meche.rpi.edu!fitz (Brian Fitzgerald) writes:
>Michael Harris writes:
>>When I am running a shell script, how can I tell when a directory is empty?
>
>Try:
>
>	if [ ! "`ls -A $name`" ] ; then

Why not just run rmdir $directory and if it works, then the directory was empty.

ie.

if [ -d test1 ]
then rmdir test1
     if [ -d test1 ]
     then echo Not empty directory test1
     else mkdir test1
          echo Empty directory test1
     fi
fi


Sincerely,
John Urban



More information about the Comp.unix.questions mailing list