--- title Something Innteresting I noticed about directories in unix ---

Something cool I noticed

TL;DR Directories all have nlinks > 1. More specifically, each directory has exactly 2 + count(children). The hardlinks to a directory are:

  1. its named entry under its parent directory
  2. its . entry under itself
  3. its .. entry under each of its child directories

I found this because I recently reimplemented my own ls that puts things in, at least in my opinion, a more obvious format when using the -l (long) option. Instead of just dumping a bunch of undescribed numbers into stdout (which I imagine is nicer to parse via awk or whatever a real shell-based Sicko would use), my implementation prints stuff in a format kind of like:

drwxr-xr-x ino4611592 x04 jams
drwxr-xr-x ino4520375 x03 notes
drwxr-xr-x ino4520534 x02 old
lrwxr-xr-x ino4693395 x01 usb -> example
drwxr-xr-x ino4611844 x02 screenshots
drwxr-xr-x ino4432523 x27 src

I've been implementing things only exactly as I find them useful, I'll probably get around to adding everything else eventually. Oh and of course the whole reason I did this all is because I wanted type-based coloring, which OpenBSD's ls does not abide. Also mine is only about 400 lines and only depends on standard libc so that's also way better for my taste.

BUT ANYWAY, the thing I was here to post about. You may notice that all those example directories have "x03" after the INOde number, that's the number of hardlinks, how many times (x) the inode is used throughout the filesystem. Why isn't that one for directories? It's because of the . and .. entries that refer to that directory! Like I said in the TL;DR. :P