1

I tried using relative to pwd, but it doesn't work.

My current pwd is different from target path and original path (from where link need to be created).

I do not know the home directory so I cannot use relative-to-home pathname. Absolute pathname is not required.

How can I create soft link using relative pathname?

2 Answers 2

3

A relative symlink is relative to its parent directory, that's irrelevant from the current directory of any process or the home directory of any user.

For instance, if /some/dir/link is a symlink to ../foo/bar, then that's a symlink to /some/dir/../foo/bar (/some/foo/bar if /some/dir is not itself a symlink), regardless of what your current or home directory is.

If you want to create a new symlink to a file relative to the current directory (for instance the bin/ls file in your current /home/stephane directory), then you can do (in POSIX-like shells or fish):

ln -s -- "$PWD/bin/ls" /some/dir/link

Which would create an absolute symlink (to /home/stephane/bin/ls). To create a relative symlink, you'd need:

ln -s ../../home/stephane/bin/ls /some/dir/link

(assuming /some and /some/dir are not themselves symlinks)

GNU ln has a -r option to help you compute that path:

ln -rs bin/ls /some/dir/link

(which would create the same symlink to ../../home/stephane/bin/ls).

1

You can do symlink using relative path

e.g. (you want to create symbolic link from folder bb which is located in ../../bb related to the current working directory to mylink

ln -s ../../bb/ mylink

$ ls -lsa mylink
0 lrwxrwxrwx 1 user user 9 Apr 18 15:51 mylink -> ../../bb/

Another example where mylink is in a related folder for pwd

mkdir ../../tst
$ ln -s ../../bb/ ../../tst/mylink
$ ls -lsa ../../tst/mylink
0 lrwxrwxrwx 1 user user 9 Apr 18 16:08 ../../tst/mylink -> ../../bb/
8
  • The path for 'mylink' should also be relative to pwd. It works in the case where we know the full path. But, I dont know the full path to the location, not even home directory. Commented Apr 18, 2017 at 12:56
  • the path to mylink is indeed related to pwd. In the example i provided I didn't use full path nor home directory
    – Yaron
    Commented Apr 18, 2017 at 12:58
  • The location where link needs to be created is not in pwd, it is outside pwd. Commented Apr 18, 2017 at 13:01
  • @HarshPatel - I've added another example, where the target link is related to pwd
    – Yaron
    Commented Apr 18, 2017 at 13:04
  • I appreciate, but the location where it needs to created is outside pwd. For example ./../../location. Commented Apr 18, 2017 at 13:08

You must log in to answer this question.

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