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

Brian Fitzgerald fitz at mml0.meche.rpi.edu
Mon Apr 1 16:03:52 AEST 1991


>>Michael Harris writes:
>>>When I am running a shell script, how can I tell when a directory is empty?

I replied:
>>Try:
>>
>>	if [ ! "`ls -A $name`" ] ; then

Michael Stefanik writes:
>$ ls -A /tmp
>ls: illegal option -- A
>usage: ls -RadCxmnlogrtucpFbqisfLk [files]
>
>How about this instead?
>
>	if [ `ls $dir | wc -l` = 0 ] ; then
>
>I think that this would work rather consistently, regardless of the
>pecularities of /bin/ls.

%-(

[22] mml0 fitz /tmp% mkdir foo
[23] mml0 fitz /tmp% ls foo
[24] mml0 fitz /tmp% touch foo/.hidden
[25] mml0 fitz /tmp% ls foo
[26] mml0 fitz /tmp%

#-)

[30] mml0# cd /tmp
/tmp
[31] mml0# ls foo
.hidden



More information about the Comp.unix.questions mailing list