I assume that your question is how bash can know to change the directory so that the working directory is foo
(rather than foo/bar/..
) in cd foo/bar/..
.
While these two paths will be are represented by the same inode (unless there are symlinks present in the path, as shown at the end of this answer), the shell does need to take special measures to show the current directory name as foo
, rather than ..
. In bash
, when cd
encounters ..
in the path, it internally just strips the parent directory away, meaning that ..
can never be the directory name.
This is documented in help cd
:
..
is processed by removing the immediately previous pathname component back to a slash or the beginning of DIR.
This special casing results in the following interesting behaviour (note that foo/qux/..
still resolved to foo
, even when the real path was bar/baz/..
):
$ tree
.
|-- bar
| `-- baz
`-- foo
3 directories, 0 files
$ ln -s "$(readlink -f bar/baz)" foo/qux
$ tree
.
|-- bar
| `-- baz
`-- foo
`-- qux -> bar/baz
4 directories, 0 files
$ cd foo/qux/..
$ basename "$(pwd)"
foo
ls -a
if you look then both.
and..
exist in the current directory. – May 07 '15 at 11:57..
refers to the parent directory. To change to the 'previous' directory you can usecd -
– Lambert May 07 '15 at 12:00..
is not a hardlink to the previous directory as stated by James. – Lambert May 07 '15 at 12:04cd foo/bar; cd ..
-- this results in the shell stating the directory is "foo" in the prompt, but how does it know thatcd ..
went to the same directory as "foo"? – Chris Down May 07 '15 at 13:04cd ..
doesn't need to know any name information, the shell does have to take some special casing to know that..
==foo
. – Chris Down May 07 '15 at 13:11foo
and..
have the same inode, did you even read what i linked? Read the bit that saysInode Structure of a Directory:
– May 07 '15 at 13:14