Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
6  
No answer, but here is a good associated read, Rob Pike on G+ discussing the origin of the dotfile(s). –  bdowning Apr 22 at 20:24

7 Answers 7

up vote 52 down vote accepted

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.

(Source)

share|improve this answer

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

share|improve this answer
13  
The question asks why the .. directory entry is there. –  kundor Apr 22 at 15:52
2  
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 at 10:48

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.

share|improve this answer
    
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 at 4: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 at 6:12

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.

share|improve this answer
9  
What's wrong with cd /? –  Bach Apr 23 at 7: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. –  SkylarMT Apr 23 at 7:18
2  
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 at 0:58
4  
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 at 4: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 at 20:05

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

$ readlink -f ..
/home

$ readlink -f /.
/

$ readlink -f /..
/

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

share|improve this answer

I had heard that / was a special case (no dot-dot). I suspect that looking at the system call, chroot and the command command might provide some insight.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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