14

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

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

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

  • 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 '16 at 12:36
  • This is also explains the seconds behaviour. Thank you very much. – user98456 Dec 25 '16 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 '16 at 23:04
-2

A directory is a list of files. On such list, there is nearly always two files: . and ... The first is the directory itself (so it contain some data about directory permissions), and the second is the parent directory. This make easier to go back to precious directories, without need to rescan the list of directories.

These two entries are created on directory creation, together with the directory name and link on the parent directory.

  • 5
    That just answers the question in the first part of the title, not the actual main question being asked, for which one needs to read beyond the title. – JdeBP Dec 25 '16 at 12:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.