Why does the root directory '/' have a reference to its "parent"?

All we need is an easy explanation of the problem, so here it is.

I’m currently developing a fake filesystem for a browsergame.
I recently implemented “..” and “.” folders so every folder has a reference to its parent.
Then I checked in my terminal if “/” also has these folders.
Actually I was very surprised that it has a directory “..” which obviously is a reference to itself.

Is the reason for this consistency or is there a better explanation?

I’m basically looking for the document where this is documented.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

There is this POSIX definition :

3.144 Empty Directory

A directory that contains, at most, directory entries for dot and
dot-dot, and has exactly one link to it (other than its own dot entry, if one exists), in dot-dot.

This definition is repeated with different phrasings in the POSIX descriptions of all file-commands.
For example for rmdir :

The definition of an empty directory is one that contains, at most,
directory entries for dot and dot-dot.

This above convoluted definition for an Empty Directory is apparently behind this funny convention, and its purpose is to avoid any exceptions to the rule, not even for slash (/).

Method 2

The Single UNIX Specification states:

As a special case, in the root directory, dot-dot may refer to the root directory itself.

No reason is stated in the standard. For an official “why?” answer, you may need to ask Ken Thompson directly.

Method 3

File system traversal.

When you are looking at a file name on Unix systems, you’re looking at a rooted tree.

When you’re looking at a file, it is in a directory. You can ask that directory what it is, by going up a level (..), and checking for the inode of the directory you’re in. You then repeat, and you can build up where you are. But when you reach the root, there is no up. There is only ‘here’. By setting ‘.’ and ‘..’ to the same value, you set a unique signal that no other directory in that file system can have. It is the root.

When you mount a file system to another mount point – you have a /home filesystem on another disk, for example, you override the ‘..’ with a reference to the mount point on the root file system. So the mounted file system root that used to have a ‘.’ and ‘..’ that were the same, now have different values.

Having ‘.’ and ‘..’ repeat the same data is an important, unique signal, true only for the top of the tree. And that tells traversal programs they can stop looking for parent nodes.

I think there is tangential documentation in the Lions Commentary To The Unix Kernel version 6. Around Page 84, where it describes how the mount point is handled.

Method 4

I like the other answers given, and would add that part of what makes this make sense (to me at least) is that directories are, in fact, special types of files. Any “special” type of file in UNIX has required attributes that make it conform to its “special” type classification. Without ALL these attributes in place, the system might not recognize it for what it is supposed to be or might misinterpret it. You can view a directory as a file using, say, vim. If you look at just about any directory in your system with vim, for example,

$ vim ~

you’ll see that the first uncommented thing listed in that “file” is “../”, i.e., the first thing listed in this special file is the parent directory, and as you expect, its parent is one level “up”. But if you instead try

$ vim /

you’ll see on the first uncommented line that it says “./” instead of “../”. Why the difference and why does it have to be here? “/” has to have a “parent” listed to conform with the requirements of being recognized as a special file of type “directory.” Without this listing first, it would be as if “/” had a different parent specified (i.e., whatever came first in the “/” special file), and I suspect (but have not confirmed) that this would cause one of its children to be interpreted as its parent, creating a circularity in the file system, i.e., it would NOT be a rooted filesystem anymore, but rather a circular one! In other words, if the system is going to always interpret the first entry in a “directory file” as “this directory’s parent”, then “/” has to have an appropriate first listing to be interpreted properly.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply