46

Why cd .., typed at root folder, does not warn or fails with an error?

I would expect:

/$ cd ..
-bash: cd: ..: No such file or directory

Instead, I'm left at /. Of course, this is since .. does exist in /, and is simply /, just like .. I just wonder why it is like that.

Bach
  • 849

6 Answers6

54

According to the Open Group (responsible for the POSIX standard):

Each directory has exactly one parent directory which is represented by the name dot-dot in the first directory. [...] What the filename dot-dot refers to relative to the root directory is implementation-defined. In Version 7 it refers to the root directory itself; this is the behavior mentioned in POSIX.1-2008. In some networked systems the construction /../hostname/ is used to refer to the root directory of another host, and POSIX.1 permits this behavior.

A.4.13 Pathname Resolution

The dot-dot entry in the root directory is interpreted to mean the root directory itself. Thus, dot-dot cannot be used to access files outside the subtree rooted at the root directory.

chroot - change root directory

Paulo Tomé
  • 3,782
Twinkles
  • 774
29

You don't get an error because even the / directory actually has a valid directory entry for .., but unlike with other directories it points to the directory itself and thus behaves identical to .:

$ ls -lid / /. /..
128 drwxr-xr-x 22 root root 4096 Apr 15 11:26 /
128 drwxr-xr-x 22 root root 4096 Apr 15 11:26 /.
128 drwxr-xr-x 22 root root 4096 Apr 15 11:26 /..
$

As the first column tells you, ., .., and / all have the same inode-number and thus are the same filesystem entries.

So even if you cd .. inside of / you just stay in /.

Andreas Wiese
  • 10,400
  • 13
    The question asks why the .. directory entry is there. – Nick Matteo Apr 22 '14 at 15:52
  • 3
    The only reason that comes to my mind, is that you want to treat / just like any other folder, including a valid entry for ... Otherwise you would have to treat / differently compared to all other folders in the system. – Dohn Joe Apr 24 '14 at 10:48
16

It's there because removing it would require creating special-case handling code in the kernel and the C libraries. Right now you can assume that there will always be a . and .. in any directory you go to.

The only special-case code required right now is in filesystem mounting code, where the code overrides the inode value of .. to point to the directory containing the mount point, since root directories aren't always root directories.

user65905
  • 161
  • After all, it is impossible for 'parent-referral' in tree structures to be free of any special-case handling, isn't it? The root has to be special by definition, and any attempt to mask that will lead to special cases or confusion elsewhere. – musiphil Apr 24 '14 at 04:35
  • @musiphil In unix the special cases here are shuffled off to the mount command and its associated special file /etc/fstab. – luser droog Apr 24 '14 at 06:12
2

Another reason '..' is in / is that if it wasn't there it would create another special case: root directory would have one hard link fewer than all the other directories (all directory nodes have n+2 links, where n is the number of direct subdirectories inside). This would break various programs that rely on in for optimization of directory scans.

cov
  • 21
1

Another way to check what . and .. in / really is:

$ readlink -f ..
/home

$ readlink -f /.
/

$ readlink -f /..
/

As you can see, . and .. is pointed to /.

cuonglm
  • 153,898
0

It's nice that it's there, because otherwise I wouldn't be able to spam ../../../../ to get to the root from a folder. If it didn't allow the .. at the root then I'd be stuck counting the number of folders down the working dir is.

  • 12
    What's wrong with cd /? – Bach Apr 23 '14 at 07:02
  • @Bach Sometimes, keyboard mashing is more fun. Besides, in many circumstances you're not trying to go to the root, in which case using ../ is better. I also do a lot of file operations in PHP, where I can't do / for several reasons. – Skylar Ittner Apr 23 '14 at 07:18
  • 3
    The flip side is that maybe you accidentally enter too many ../s and operate on (or remove) a file you didn't intend to without error. – jamesdlin Apr 24 '14 at 00:58
  • 7
    Repeating ../ more times than needed and wishing it to work as intended looks like a (dormant) logic error to me, even if the filesystem apparently allows it. – musiphil Apr 24 '14 at 04:40
  • @musiphil typing spaces in between commands - is a logic error (assumption that all spaces between tail -f | grep are a must). Most unix folks suffer from it. Typing '../..' looks like a Windows-folk problem (by default, cmd starts in X:\Windows and you have to spam .. from the beginning). Why to heal a small problem when there's a larger one? (spaces) – kagali-san Apr 24 '14 at 20:05
  • btw, I think that ../.. with relative_to_/_depth+1 - must redirect back to current directory (imagine broke symlinks fun after that one) – kagali-san Apr 24 '14 at 20:07
  • 2
    If root didn't have .., you could just do while [ -d .. ]; do cd ..; done – celtschk Apr 25 '14 at 09:09