1

See the Unix commands below. When we are dealing with soft-links, there can be multiple paths to the root from each directory. So in this case, how is pwd and cd .. calculated? It means that directory paths are no longer stateless, right?

$ cd ~
$ mkdir a b
$ cd a
$ ln -s ~/b b
$ cd b
$ pwd
/home/myuser/a/b

$ cd ..
$ pwd
/home/myuser/a
1
  • This kind of question would be on topic on Unix & Linux.
    – terdon
    Oct 4, 2014 at 14:29

2 Answers 2

2

Look at this example:

[myuser@test ~]$ pwd
/home/myuser
[myuser@test ~]$ mkdir a b
[myuser@test ~]$ cd a
[myuser@test a]$ ln -s ~/b b
[myuser@test a]$ cd b
[myuser@test b]$ pwd
/home/myuser/a/b
[myuser@test b]$ pwd -P 
/home/myuser/b
[myuser@test b]$ echo $$ 
2432
[myuser@test b]$ ls -l /proc/2432/cwd
lrwxrwxrwx 1 myuser myuser 0 Oct  4 04:10 /proc/2432/cwd -> /home/myuser/b
[myuser@test b]$ 
[myuser@test b]$ 
[myuser@test b]$ pwd
/home/myuser/a/b
[myuser@test b]$ cd -P .. 
[myuser@test ~]$ pwd
/home/myuser
[myuser@test ~]$ 
[myuser@test ~]$ env | grep "PWD"
PWD=/home/myuser
OLDPWD=/home/myuser/a/b

See option -P to cd from bash manual:

-P      If set, the shell does not follow symbolic links when executing commands such  as  cd  that
                  change  the  current  working directory.  It uses the physical directory structure instead.
                  By default, bash follows the logical chain of directories when  performing  commands  which
                  change the current directory.

As you can see, the current dir keeps by kernel is your real dir (/proc/2432/cwd -> /home/myuser/b) but bash can do whatever wants follow symbolic links or not, because cd is bash internal command.

0

The commands are always computed based on the actual(resolved) directory behind the soft link. When you do

$ cd b   ; you end up in the directory pointed by b

Any command from here is resolved based on this new location

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