Quantcast
Viewing all articles
Browse latest Browse all 4

Answer by Kamil Maciorowski for Linux symbolic links: how to go to the pointed to directory?

In bash the cd builtin uses -P and -L switches; pwd understands them in the same way:

user@host:~$ ln -s /bin foobaruser@host:~$ cd -L foobar      # follow linkuser@host:~/foobar$ pwd -L     # print $PWD/home/user/foobaruser@host:~/foobar$ pwd -P     # print physical directory/binuser@host:~/foobar$ cd -       # return to previous directory/home/useruser@host:~$ cd -P foobar      # use physical directory structureuser@host:/bin$ pwd -L         # print $PWD/binuser@host:/bin$ pwd -P         # print physical directory/bin

Moreover cd .. may be tricky:

user@host:/bin$ cduser@host:~$ cd -L foobaruser@host:~/foobar$ cd -L ..   # go up, to ~/user@host:~$ cd -L foobaruser@host:~/foobar$ cd -P ..   # go up, but not to ~/user@host:/$

See help cd and help pwd. Note that you may also have an executable (i.e. not a shell builtin) like /bin/pwd that should behave similarly. In my Kubuntu the difference is the pwd builtin without any option uses -L while /bin/pwd by default uses -P.

You can adjust the default behavior of cd builtin by set -P (cd acts as cd -P) and set +P (cd acts as cd -L). See help set for details.


Viewing all articles
Browse latest Browse all 4

Trending Articles