Newsgroups: comp.unix.questions Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!snorkelwacker.mit.edu!bloom-picayune.mit.edu!athena.mit.edu!jik From: jik@athena.mit.edu (Jonathan I. Kamens) Subject: Re: How do I tell when a directory is empty in a script? Message-ID: <1991Apr3.142150.4445@athena.mit.edu> Sender: news@athena.mit.edu (News system) Organization: Massachusetts Institute of Technology References: <1991Mar30.040400.13893@ncsu.edu> <1991Mar30.225406.20493@dg-rtp.dg.com> Date: Wed, 3 Apr 91 14:21:50 GMT Lines: 48 In article <1991Mar30.225406.20493@dg-rtp.dg.com>, hunt@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. I just did the following tests on both a BSD-derived and a SysV-derived system: 1) Create a new, empty directory. Check its size. Cd into it and run the following: if [ -s . ]; then echo "Directory has some files in it." else echo "Directory is empty." fi and see what happens. 2) Create a bunch of files in the directory. Remove them all. Check the directory's size again. In the first case, the size of the newly created directory was non-zero, just as I expected, and the if statement printed out "Directory has some files in it," although I hadn't added any. How can the directory be empty when it has to store entries for "." and ".." in it? 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. If you tried what you posted on a system and it worked, I'd be very interested in knowing what kind of Unix it was and what type of filesystem it's running. Does it store the "." and ".." directory entries somewhere special, or does it fake them without putting them in the filesystem, or does "size" mean something special in the context of directories, or what? -- Jonathan Kamens USnail: MIT Project Athena 11 Ashford Terrace jik@Athena.MIT.EDU Allston, MA 02134 Office: 617-253-8085 Home: 617-782-0710