How is "/a/./b/../../c/"
equal to /c
?
I saw this as a question on one of the Stack Exchange sites. Apparently ..
means to pop the stack(?). Why is this the case?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityHow is "/a/./b/../../c/"
equal to /c
?
I saw this as a question on one of the Stack Exchange sites. Apparently ..
means to pop the stack(?). Why is this the case?
Assume root looks like:
/a/b
/c
Let's break it down to componenets:
/
-> root
/a
-> in (a)
.
-> THIS dir path
/a/./
-> still in /a
/a/./b
-> in /a/b
..
-> go "up" one level
/a/./b/..
-> /a/b/..
-> /a
/a/./b/../..
-> /a/..
-> /
/a/./b/../../c
-> /c
In the *nix world, every directory is a child directory of a parent directory. Every directory has an implicit '.' directory that refers to itself, and an implicit '..' directory that refers to its parent. So if you're in a particular directory, and you change directory (cd
) to '.' (commands starting with '$' can be run on your command line!), you'll stay in the same directory:
$ pwd
/tmp/a/b
$ cd .
$ pwd
/tmp/a/b
But changing directory to '..' goes up one:
$ cd ..
$ pwd
/tmp/a
And changing directory to '..' goes up again:
$ cd ..
$ pwd
/tmp
The only directory that is an exception to this rule is the root directory, which doesn't have a parent directory:
$ cd ..
$ pwd
/
$ cd ..
$ pwd
/
Check out this tutorial on Unix directories for more details.
/ is your root directory. It has a directory structure like,
/
___ /c
___ /a
______/b (child dir of a)
___ /other dirs
Now , a single dot . means the same directory and double dots (..) Means the parent directory.
So in your example , when you use . in the path it stays in the same directory and when you use .. It jumps back to its parent directory. So eventually by jumping up, it reaches the root (/) so it's equivalent to /c