18

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!?

1
  • Note that symbolic links aren't shortcuts; they're more like pointers.
    – wizzwizz4
    Dec 26, 2016 at 9:32

1 Answer 1

37

.. 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.

3
  • Aha, the confusion comes from using cd to check. So, correct me if I am wrong, .. always points to the physical parent, If a symlink uses .. it will be directed to the actual (physical) parent. The confusion happends only with cding into directories.
    – user98456
    Dec 25, 2016 at 12:36
  • This is also explains the seconds behaviour. Thank you very much.
    – user98456
    Dec 25, 2016 at 12:37
  • 3
    @Theophrastus, ls -lai shows you that /.. is the root directory itself, again (a.k.a. /. or /). Unless the file system is broken, there should be no other case where . == ..; even when a fs is mounted somewhere it gets the correct .. of the (temporary) parent (so there is some magic going on there, .. is not just a simple, static entry in the file system table).
    – AnoE
    Dec 25, 2016 at 23:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .