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 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.
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; }
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`;
}