All symbolic links are one-way. As far as the kernel is concerned, after going into /D/S1
and running chdir("ls2")
, you're in /D/S2
, so if you run chdir("..")
, you end up in /D
.
If you do this in a shell, after
cd /D/S1
cd ls2
cd ..
you end up in /D/S1
. The reason is that the shell does its own tracking of the current directory, and it remembers symbolic links.
You can't disable this shell behavior on a link-by-link basis, but you can disable it when you run the cd
command. After running cd ls2
, the shell remembers the current directory as /D/S1/ls2
:
$ pwd
/D/S1
$ cd ls2
$ pwd
/D/S1/ls2
$ cd ..
$ pwd
/D/S1
To instruct the shell to forget its symlink-aware current directory tracking, pass the -P
option to cd
. The pwd
command also has a -P
option.
$ pwd
/D/S1
$ cd ls2
$ pwd
/D/S1/ls2
$ pwd -P
/D/S2
$ cd -P ..
$ pwd
/D
You can also forget the logical tracking when you change into the symlink:
$ pwd
/D/S1
$ cd -P ls2
$ pwd
/D/S2
$ cd ..
$ pwd
/D
cd -P
to change directory.