Create an absolute symbolic link to the current directory

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?

Asked By: Tim

||

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.

Answered By: Michael Homer

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.

Answered By: cuonglm
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.