Dot Dot (..) in Filesystems – Behavior with Symlinks Explained

cd-commanddirectoryfilenamesfilesystemssymlink

I am trying to understand the nature of .. (double dot). It seems to point to different targets depending on how you access it.

Example:-

    /outer/
        middle/
            inner/
        inner --> ./middle/inner/

Now, /outer/middle/inner/.. leads to /outer/middle/, but if I access it through the symbolic link, /outer/inner/.. leads to /outer/. It seems that there are two different .. here.

What is .. exactly? symbolic link? hard link? Or is it dynamically generated depending on the path when the directory is accessed? Or perhaps my understanding of symbolic links is wrong.

Edit:

Other related observation

    /outer/
        middle/
            file
            inner/
               f --> ./../file
        inner --> ./middle/inner/

Now if I try to access file through /outer/inner/f it works. But it shouldn't since /outer/inner/../file does not point to a file. the file is in /outer/middle/inner/... This seem to contradict the behavior in above!?

Best Answer

.. is a hard link to the parent directory which is created as part of the directory entry.

If you issue ls -ail in each of these directories, you should see that the following entries all have the same inode (first field) and hard link count (third field):

  • .. when executing ls -ail in inner
  • . when executing ls -ail in middle
  • middle when executing ls -ail in outer

Now, /outer/middle/inner/.. leads to /outer/middle/, but if I access it through the symbolic link, /outer/inner/.. leads to /outer/. It seems that there are two different .. here.

There is one .. entry in inner. If your current working directory is outer, you should see the same results (a directory listing of middle) from

  • ls -al inner/..
  • ls -al middle/inner/..

The only situation in which "accessing" .. through the symbolic link should provide different behavior is if you cd into inner using the symbolic link, so that your logical working directory (pwd -L) differs from your physical working directory (pwd -P). In this case, cd .. will take you back to outer not because there is a different .. entry, but because your shell is keeping track of your logical working directory and popping you out one level, rather than referencing the actual .. entry in inner.

This is a convenience provided by, for example bash as part of the built-in cd command. You can override this by asking it to change to the actual .. entry with

cd -P ..

where -P instructs cd (as with pwd above) to use the physical path.

Related Question