Linux Symbolic Links – Navigate to Directory Pointed by Symbolic Link

linuxsymbolic-linkxfce

I have a project in its own directory:

/dir/to/project/

I have a symbolic link to that directory on the desktop:

/home/user/Desktop/project/

When I double click on that link, the directory window that opens is:

/home/user/Desktop/project/

instead of the real one, /dir/to/project.

The same happens with the command line (Bash).

Is it possible to get what I wish, i.e. go to the directory pointed to, instead of the symbolic one?

Note: the windows environment I am using now is Xfce, but I am also interested in a generic answer.

Best Answer

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

user@host:~$ ln -s /bin foobar
user@host:~$ cd -L foobar      # follow link
user@host:~/foobar$ pwd -L     # print $PWD
/home/user/foobar
user@host:~/foobar$ pwd -P     # print physical directory
/bin
user@host:~/foobar$ cd -       # return to previous directory
/home/user
user@host:~$ cd -P foobar      # use physical directory structure
user@host:/bin$ pwd -L         # print $PWD
/bin
user@host:/bin$ pwd -P         # print physical directory
/bin

Moreover cd .. may be tricky:

user@host:/bin$ cd
user@host:~$ cd -L foobar
user@host:~/foobar$ cd -L ..   # go up, to ~/
user@host:~$ cd -L foobar
user@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.

Related Question