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

Bob Goudreau goudreau at larrybud.rtp.dg.com
Fri Apr 5 04:42:59 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:
> |> 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....
> How can the directory be empty when it
> has to store entries for "." and ".." in it?

Because the directory does not *have* to store entries for "." and
".." in it.  That's just an implementation detail; various UNIXes
choose to implement things differently.  Remember, POSIX.1 doesn't
require anything particular about the implementation of directories;
only the *interfaces* are specified (readdir, mkdir, etc.).  Greg's
example was on a system running DG/UX, which happens to implement
directory files by keeping "." and ".." info in the inode itself.
The implementation is unimportant, however -- all the standard
interfaces (POSIX.1's readdir(), SVR4's getdents(), etc.) all return
"." and ".." info as if there actually were on-disk entries for those
names.  As Greg noted, check your system's documentation if you are
interested in the details of implementation. 


>   In the second case, the directory did not shrink when the files were
> deleted.  Directories grow.  I'm not even convinced that fsck shrinks
> empty dirctories; I believe the only way to shrink a directory with
> lots of empty entries in it is to create a new directory, mv the
> files from the old directory into the one one, and remove the old
> directory.

Again, this sort of behavior is implementation-specific.  Check your
system documentation.  DG/UX happens to be able to shrink directories
dynamically; other systems differ.

----------------------------------------------------------------------
Bob Goudreau				+1 919 248 6231
Data General Corporation		goudreau at dg-rtp.dg.com
62 Alexander Drive			...!mcnc!rti!xyzzy!goudreau
Research Triangle Park, NC  27709, USA



More information about the Comp.unix.questions mailing list