Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I need a symlink that resolves relative to the directory it is placed in. What command is used to create such a thing?

share|improve this question
  1. Go to the directory you want the link to reside in
  2. Run the command ln -s ../some/other/file linkname

The path you provide is stored with the file. When you access the file the stored path is looked up and expanded relative to the file. It does not know what directory you were in when you created the link.

share|improve this answer
14  
No you don't. You just have to make sure that you specify the location relative to the link name instead of your current directory. – Ignacio Vazquez-Abrams May 28 '10 at 8:26
2  
To help in understanding: The path you provide is stored with the file. When you access the file the stored path is looked up and expanded relative to the file. It does not know what directory you were in when you created the link. – Marian May 30 '10 at 14:27
    
exactly equivalent: ln -s ../some/other/file /some/dir/linkname – sehe Dec 22 '11 at 12:07
    
Instead of going down a directory and the up again, you can also just do ln -s ./file linkname since . refers to the current directory. – pduersteler Mar 12 '13 at 13:36

Recent versions of GNU coreutils' ln (>= 8.16) support the --relative (or -r) option which means you can call ln -s with 2 absolute or relative (in respect to your working directory) paths and it will figure out the correct relative path that has to be written to the symlink.

share|improve this answer
    
Thanks. The -r option is helpful when you are cross-compiling and need absolute paths on the build host but relative paths on the target. – Robert Calhoun Sep 1 '15 at 15:57

Relative links were tricky for me on OS X, i.e.

~/Dropbox/git/dave-bot $ ln -s ../codyhess/bin ~/bin
~/Dropbox/git/dave-bot $ ln -s ../codyhess/bin/ ~/bin

both did not work (something was created but it wasn't a directory). I created the desired link by using absolute paths.

~/Dropbox/git/dave-bot $ ln -s ~/Dropbox/git/codyhess/bin/ ~/bin
share|improve this answer
    
It's a viable solution, so doesn't need to be a comment. This can stay as a separate answer. – slhck Oct 5 '11 at 20:43

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.