11

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.

1

3 Answers 3

21

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.

7

Use readlink to resolve the link to its target:

cd $(readlink thelink)

You may want to define a function in your bash profile:

function cdl { local dir=$(readlink -e $1); [[ -n "$dir" ]] && cd $dir; }
2
  • @KamilMaciorowski - Yours is the best approach. If you write it as an answer (with details about how to do the same thing from the GUI), I will tick it as the accepted one.
    – Pietro
    Apr 9, 2018 at 14:08
  • @KamilMaciorowski - OK about GUIs' behaviour. Again, if you rewrite your comment as an answer, I will accept it.
    – Pietro
    Apr 9, 2018 at 14:41
1

I don't know how to achieve it in GUI, but there is a workaround in Command line.

Say your symbolic link is :

/home/user/Desktop/project/

then, You can use readlink command to get resolved symbolic link or it's canonical file name. Then just cd to it.

cd `readlink /home/user/Desktop/project`

Here, readlink resolves the linkname and then passes to cd using substitution.

If you are already in the Desktop folder, then there is no need to specify absolute path, just project will do

cd `readlink project`

If you visit this folder frequently, you can write a one line function for it in bash :

function cdproject
{
    cd `readlink home/user/Desktop/project`;
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .