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

Dave Turner dmturne at PacBell.COM
Wed Apr 3 03:19:53 AEST 1991


In article <=-#g4!g at rpi.edu> fitz at mml0.meche.rpi.edu (Brian Fitzgerald) writes:
>>>Michael Harris writes:
>>>>When I am running a shell script, how can I tell when a directory is empty?
>>
>>	if [ `ls $dir | wc -l` = 0 ] ; then
>>

Use the form of ls that gives all entries in  a directory including . and .. .
Check for a count of two.

For System V and the Bourne shell

	if [ `ls -a $dir | wc -l` -eq 2 ]
	then
		echo empty
	else
		echo not empty
	fi



-- 
Dave Turner	415/823-2001	{att,bellcore,sun,ames,decwrl}!pacbell!dmturne



More information about the Comp.unix.questions mailing list