11

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.

2 Answers 2

15

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/
3
  • 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. May 15, 2012 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, 2012 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, 2012 at 14:41
4

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

5
  • 4
    Latest coreutils can automatically create relative symlinks using ln -s -r foo ~/bar. May 15, 2012 at 14:30
  • @grawity: Interesting. Not on my system yet, apparently (Debian Sid). May 15, 2012 at 14:33
  • It seems to have been added for v8.16 on March 22nd, according to git. May 15, 2012 at 14:36
  • 1
    Also, what is the advantage of $(readlink -f .) over $PWD? May 15, 2012 at 14:36
  • 2
    @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. May 15, 2012 at 14:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .