I am now under a directory with very long path. For future visiting it quicker, I would like to create a link to it.
I tried
ln -s . ~/mylink
~/mylink actually links to ~. So can I expand ~ into the obsolute pathname, and then give it to ln?
I am now under a directory with very long path. For future visiting it quicker, I would like to create a link to it.
I tried
ln -s . ~/mylink
~/mylink actually links to ~. So can I expand ~ into the obsolute pathname, and then give it to ln?
A symlink actually stores the path you give literally, as a string¹. That means your link ~/mylink contains "." (one character). When you access the link, that path is interpreted relative to where the link is, rather than where you were when you made the link.
Instead, you can store the actual path you want in the link:
ln -s "$(pwd)" ~/mylink
using command substitution to put the output of pwd (the working directory name) into your command line. ln sees the full path and stores it into your symlink, which will then point to the right place.
¹ More or less.
You should use:
ln -s "$(cd . && pwd)" ~/mylink
or:
ln -s "$(pwd -P)" ~/mylink
to get the right result for current working directory. It can be changed while you was working in it as in this question.