Re: Empty Directory problem

From: John (
Date: 10/28/03

Date: 27 Oct 2003 16:17:17 -0800 (newexpectuser) wrote:

> ... I just want to do the following:
> if [ $DIR is empty ]
> echo "The directory is empty" > $LOG/logfile.dat
> exit
> fi

what shell? what OS? in ksh on most unix's, if you do "ls /mydir"
and "/mydir" is empty you get a null return ... if you do "ls -l
/mydir" and it is empty, you get the message "total 0" which indicates
an empty dir ... so your test could look like this:

if [ -d $DIR ] # test to see if $DIR exists
  if [ `ls -l $DIR` == "total 0" ]
  then echo "the directory is empty" >/yourlogfile
  else echo "the dir is not empty"
else echo "$DIR does not exist"

Don't forget to read the manpage for ls to find the relevant forms for
your shell & OS.


Relevant Pages