Think of a directory as a text file that looks something like:
123:foo.txt
234:bar.txt
123:other-name-for-foo.txt
That's all there is (at least in traditional Unix file systems).
foo.txt
is not a child of that directory in that it's not an exclusive relationship. It's just that the file of inode number 123 is referenced in two entries of that directory: with names foo.txt
and other-name-for-foo.txt
.
Those two directory entries are also known as two hard links to the same file.
$ touch foo.txt bar.txt
$ mkdir other-dir
$ ln foo.txt other-name-for-foo.txt
$ ln foo.txt other-dir/whatever
$ ls -i . other-dir
.:
58892735 bar.txt 58958143 other-dir/
58879562 foo.txt 58879562 other-name-for-foo.txt
other-dir:
58879562 whatever
.
and other-dir
are two files of type directory, each containing a number of directory entries. ls
lists the entries of a directory whose names don't start with .
and with -i
includes the inode number.
With -a
(to include dotfiles), and -i
(ls -ai
), that's more or less dumping the content of that text file I was mentioning above.
You'll notice that the file of inode 58879562 above is linked once to other-dir
and twice to .
. That 58879562 file now has a link-count of 3, but neither .
nor other-dir
care about that. That information is stored in the inode of the file and is just for the system to keep track of how many times the file is linked to a directory. If that number falls to 0, the disk space occupied by that file can be reclaimed.
In addition to inode number and name, directories also contain the file type of the file (regular/fifo/directory/symlink/device..., though not for all file systems). That information can be retrieved otherwise (as it's stored in the inode) so is not necessary. It is done mainly for optimisation purposes and because it's possible. It is possible because it is not possible to change the type of a file (contrary to other metadata), so that information stored in the directory is not going to become out-of-sync with the reality.
You'll also notice that the system call to remove a file is not remove("/foo/bar")
but unlink("/foo/bar")
. That's because all it does is remove the bar
entry from the /foo
directory, it unlinks bar
from /foo
. That file may still be linked to other (or the same by a different name) directory. It's only going to be removed when its link count reaches zero (when it's no longer linked to any directory).
As to why doing an ls
doesn't update the access time of a directory, that's probably because your filesystem is mounted with the noatime
or relatime
option.
Updating the access-time of a file (directory or other) every time it is read is costly, so many systems give up on that nowadays.
Note that the system, if not the user reads directories all the time.
For instance, when accessing /foo/bar
, the system needs to read the content of /
to find foo
and then /foo
to find bar
(though most of the time that information is cached). However, in those cases, the atime is not updated (thankfully), only when user-space processes explicitely read the content of a directory like ls
does.
As to when the ctime
of a directory is updated, that's the same as for a regular file: when its content is modified (entries added or removed or renamed) or its metadata (except atime
upon reading) is modified (like upon chmod
/chown
, ACL modifications...).