Linux symbolic links: how to go to the pointed to directory?
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game 2 Looping
--
Chapters
00:00 Linux Symbolic Links: How To Go To The Pointed To Directory?
00:46 Accepted Answer Score 21
01:48 Answer 2 Score 7
02:07 Answer 3 Score 1
02:55 Thank you
--
Full question
https://superuser.com/questions/1312196/...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#linux #symboliclink #xfce
#avk47
ACCEPTED ANSWER
Score 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.
ANSWER 2
Score 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; }
ANSWER 3
Score 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`; }