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

Steve Gombosi sog at bierstadt.scd.ucar.edu
Sun Mar 31 12:17:58 AEST 1991


Wouldn't it be easier to pipe the output of "ls -la" through "wc -l" and
test for the output being equal to two? Since an empty directory contains
only "." and "..", and "ls -la" (at least on my system) provides a "Totals"
line, the output looks like this:

	%ls -la empty_directory
	total 2
	drwxr-xr-x  2 sog           512 Mar 30 19:07 .
	drwxr-xr-x 11 sog          1024 Mar 30 19:10 ..
	%ls -la empty_directory|wc -l
	3

So, if `ls -la directory_name|wc -l` != 3, the directory has something
in it. Of course, if it's less than three, something is REALLY screwed up...



More information about the Comp.unix.questions mailing list