Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

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/
share|improve this answer
2  
Double-quote your $ variables and command substitutions, otherwise file names with spaces will muck up the run. You also don't need the ending $(basename $PWD) (and you've forgotten a $ (and quoting :-) )) since that is the default link name anyway. –  Daniel Andersson May 15 '12 at 14:29
    
I think I was being stupid. I thought I tried the pwd approach and something went wrong. Let's put that down to an ID-ten-T error –  Gareth May 15 '12 at 14:37
    
@DanielAndersson you're right that it does not support spaces in path. Good point. so for the record I will update the answer :) –  Huygens May 15 '12 at 14:41
add comment

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).

share|improve this answer
1  
Latest coreutils can automatically create relative symlinks using ln -s -r foo ~/bar. –  grawity May 15 '12 at 14:30
    
@grawity: Interesting. Not on my system yet, apparently (Debian Sid). –  Daniel Andersson May 15 '12 at 14:33
    
It seems to have been added for v8.16 on March 22nd, according to git. –  grawity May 15 '12 at 14:36
    
Also, what is the advantage of $(readlink -f .) over $PWD? –  grawity May 15 '12 at 14:36
    
@grawity: Not much. They work differently if the current working directory has been reached by a link. readlink -f will then symlink to the absolute target path of the current directory, while $PWD will symlink to the literal directory one is currently in. What is wanted is probably different from time to time, and first and foremost probably not an issue at all. –  Daniel Andersson May 15 '12 at 14:41
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.