Shell – Create an absolute symbolic link to the current directory

cwdshellsymlink

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?

Best Answer

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.

Related Question