Results 1 to 6 of 6

Thread: HP-UX empty directories?

  1. #1
    Senior Member roswell1329's Avatar
    Join Date
    Jan 2002
    Posts
    670

    Question HP-UX empty directories?

    I'm doing some scripting on an HP-UX system, and one of the checks I'd like to make is to check whether a directory is empty or not. However, I've encountered some strange behavior. As I understand it, Unix implements the directory filetype as a binary listing (one per line) of the items contained therein, correct? A typical empty directory should then be 96 (or some other standard size depending on your distribution) bytes. As the number of files increases in the directory, the size of the directory itself should also grow, correct? On several HP-UX systems, however, I've been finding several directory names that have somewhat large sizes (13K), and yet are still empty; ls -al says total 0. When I run the strings command (which may be an HP-UX-only implementation. All it does is output the any printable strings of a binary to the screen) on the directory names I get a listing of several older files that are no longer in the directory. Any idea why the directory name isn't clearing the names out of itself as the files are deleted?
    /* You are not expected to understand this. */

  2. #2
    Senior Member
    Join Date
    Apr 2002
    Posts
    317
    the directory in question isn't symlinked to another directory is it? (ie when using ls -l the directory may appear as "/home -> /usr/home") This is the only thing I can think of off hand.
    \"I believe that you can reach the point where there is no longer any difference between developing the habit of pretending to believe and developing the habit of believing.\"


  3. #3
    Senior Member roswell1329's Avatar
    Join Date
    Jan 2002
    Posts
    670
    No, the directory is an actual directory. It may be useful to point out, however, that this directory is heavily used by a spooling program. Large numbers of files are added and removed frequently.
    /* You are not expected to understand this. */

  4. #4
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    In Unix directories are usually just files, and their size is not a useful property to measure.

    The size of a directory on a Linux ext2 filesystem seems to go up in multiples of the block size (4096 bytes on my system).

    They grow as you put more files in them, but as far as I can tell, never shrink.

    This is not necessarily a bad thing, as a directory which used to have lots of files in might have lots in again one day (like /tmp)

    You cannot usefully check for the emptyness of a directory by measuring its filesize. The file size of a directory is meaningless - some unixes might even report it as 0.

    To test whether a directory is empty you need to check its contents - if it contains nothing other than . and .. , then it's empty. AFAIK, in unix all directories always contain . and ..

  5. #5
    Senior Member roswell1329's Avatar
    Join Date
    Jan 2002
    Posts
    670
    slarty -- thanks for the input. My script now runs an ls -1 on the directory, and if nothing is returned, the test is successful. I'm still confused, however. Why wouldn't the directory shrink as files are deleted? That data is no longer relevant.
    /* You are not expected to understand this. */

  6. #6
    Senior Member
    Join Date
    Jan 2002
    Posts
    1,207
    I expect someone developing the filesystem experimented with shrinking directories and came to the conclusion that they're not worthwhile. I can think of the following possible reasons, but I may be talking out of my arse

    - If a large directory has most of its files removed, there is a good chance that many files may come back again (for instance if it's a spool directory)
    - If you shrink directories they're more likely to become fragmented (maybe)
    - Shrinking a directory takes time - when in many cases it's only going to grow again later
    - The space savings from directory shrinking are reasonably small.

    There is a detailed discussion on the Linux Kernel mailing list here

    http://www.uwsg.iu.edu/hypermail/lin...06.2/0463.html

    Which discusses it.

    Unfortunately on a proprietry OS like HP-UX, we are unlikely to be able to see such discussions, however we can assume that the same rationale holds.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •