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

Bruce Varney asg at sage.cc.purdue.edu
Thu Apr 4 02:31:06 AEST 1991


In article <1991Apr3.142150.4445 at athena.mit.edu> jik at athena.mit.edu (Jonathan I. Kamens) writes:
}In article <1991Mar30.225406.20493 at dg-rtp.dg.com>, hunt at dg-rtp.rtp.dg.com (Greg Hunt) writes:
}|> Telling whether a directory is empty from a script should be possible
}|> by looking at the size of the directory itself.  If the directory size
}|> is zero, then the directory is empty.  Otherwise, it contains some
}|> files.  You should check this out on your system to make sure it holds
}|> true for the flavor of UNIX that you're using.
}
}  Um, I can't imagine this being true for *any* remotely normal flavor of Unix.
}
You're right Jon [ :-( gasp!, did I really say that? ]
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---

then just do
isempty directory_in_question
and it will tell you if the dir is empty or not


}-- 
}Jonathan Kamens			              USnail:
						      ^^^^^^
Get the UShammer!!!
			Bruce

---------
sar.casm \'sa:r-.kaz-*m\ \sa:r-'kas-tik\ \-ti-k(*-)le-\ n [F sarcasme, fr. 
   LL sarcasmos, fr. Gk sarkasmos, fr. sarkazein to tear flesh, bite the lips 
   in rage, sneer, fr. sark-, sarx flesh; akin to Av thwar*s to cut] 1: a 
   cutting, hostile, or contemptuous remark : GIBE 2: the use of caustic or 
   ironic language - sar.cas.tic aj

                                   ###             ##
Courtesy of Bruce Varney           ###               #
aka -> The Grand Master                               #
asg at sage.cc.purdue.edu             ###    #####       #
PUCC                               ###                #
;-)                                 #                #
;'>                                #               ##



More information about the Comp.unix.questions mailing list