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

Marc Brumlik root at homer.UUCP
Fri Apr 12 03:28:56 AEST 1991


In article <9424 at mentor.cc.purdue.edu> asg at sage.cc.purdue.edu (Bruce Varney) writes:
->But why are you all making this so hard?????
->Its easy. 
->do this:
->---isempty---
->#!/bin/sh
->dir=$1
->num1=`ls -aF $dir | grep -v "/$" | wc -l`
->num2=`expr \`ls -lad $dir | cut -c12-13\` - 2 `
->num=`expr $num1 + $num2`
->if [ $num = "0" ]
->then
->   echo The directory is empty
->else
->   echo The directory is not empty
->fi
->---isempty---

it can be even easier than this...  how about:

	cd $1
	files=`echo *`
	if [ "Z$files" = "Z" ]
		then	echo "empty"
		else	echo "not empty"
	fi

--
..{util20,obdient}!homer!marc	Marc Brumlik, Tailored Software, Inc.
				Wheaton, IL	Voice: 708 668 9947



More information about the Comp.unix.questions mailing list