Symlink to current folder as absolute path without typing whole path

Symlinks are saved shortcuts for relative paths:

~/foo➤ ln -s . ~/bar/foo
~/foo➤ ls -l ~/bar
total 8
lrwxr-xr-x    1 me  me     1B 15 May 14:38 foo -> .

The file ~/bar/foo is a symlink to bars current directory, i.e. itself

What if I want to create a symlink in another directory to the directory I'm in at the time? So in the above example, I'm in ~/foo and I want to create ~/bar/foo as a symlink to ~/foo. Of course my current directory might be quite a bit longer than 5 characters and might not be so easy to type.


I would say type

ln -s $PWD ~/bar/

or

ln -s `pwd` ~/bar/

You could use also

ln -s $PWD ~/bar/

but I guess you knew that and that is not what you are looking after. So please could you update your question to explain what's your goal?

If the path contains spaces, then it would be advisable to use:

ln -s "$PWD" ~/bar/

Absolutely:

ln -s "$(readlink -f .)" ~/bar

(might be good for generality in scripts) or just

ln -s ~/foo ~/bar

Relatively:

ln -s ../foo ~/bar

Tab completion will help you type.


From grawity's comment and http://lists.gnu.org/archive/html/coreutils-announce/2012-03/msg00000.html:

* Noteworthy changes in release 8.16 (2012-03-26) [stable]

...

ln now accepts the --relative option, to generate a relative symbolic link to a target, irrespective of how the target is specified.

So with this version:

ln -s --relative . ~/bar

should work (with in practice is identical your first example, with the addition of the --relative switch).