3

consider the following :

x/
    ph/
        file1.txt -> ./../file1.txt
    file1.txt
y/
    ph/ -> /x/ph/ (this is full absolute path of x/ph)
    file1.txt

/y/ph is a symlink for ph folder in x. and file1.txtis a relative symlinks.

it works correctly in x folder however if you open /y/ph/file1.txt instead of opening /y/file1.txt it opens /x/file1.txt

the goal here is to have the same directory (lets say a program) in multiple places with different config files without having to have multiple copies of the program.

according to ln's help

Symbolic links can hold arbitrary text; if later resolved, a relative link is interpreted in relation to its parent directory.

so relative links are relative to the actual folder's path not the current working directory. the question is : is there any workarounds for this ?

test case:

mkdir x y x/ph
echo x1 > ./x/file1.txt
echo y1 > ./y/file1.txt
ln -s $(pwd)/x/ph ./y/ph
cd ./x/ph
ln -s ./../file1.txt
cd ../..
cat ./x/ph/file1.txt
cat ./y/ph/file1.txt

expected result is x1 y1 but you get x1 x1

Edit:

to make the question clearer, run the test case in an empty folder. and try the following commands in the folder:

bor@borpc:~/tmp$ readlink -f ./x/ph/file.txt
/home/bor/tmp/x/ph/file.txt
bor@borpc:~/tmp$ readlink -f ./y/ph/file.txt
/home/bor/tmp/x/ph/file.txt
--------------^

i want that x there to be y. that is to make the relative symlink resolve based on pwd (current working directory or path used to access it) rather than the actual path of the actual file.

if it is possible how would one achieve such behavior.

15
  • I am confused also. I think the expected answer is x1 x1 which is what you get. Apr 24, 2015 at 14:43
  • @DougSmythies i know that linux gives x1 x1 and it is according to the specifications . the question is how to get x1 y1 ?
    – Bor691
    Apr 24, 2015 at 14:55
  • 1
    This is very ironic that you use x and y names for your folders. Because there is known problem called XY problem. Could you please explain Why you want it, and also add more description what exactly do you want. In other words explain what problem are you trying to solve.
    – c0rp
    Apr 24, 2015 at 14:59
  • 1
    @c0rp i see your point about the xy problem. there sure exists other ways to solve the problem and we did in fact solve it some other way , but since i was the one that suggested this method and it didn't work , i wanted to know if it is possible at all (even with a workaround) or not.
    – Bor691
    Apr 24, 2015 at 15:06
  • 1
    +1, I understand what the OP is trying to do. It may be an XY problem, there's other solutions, but the answer is "no, not with symlinks but you can with bind mounts"
    – Josh
    Mar 19, 2021 at 0:59

2 Answers 2

0

I'm a bit confused. You might make a symbolic link beginning at root directory (I mean /). For example,

ln -s /root/try/d s

ls -l  
total 0  

-rw-r--r-- 1 root root  0 Apr 24 16:15 d  
lrwxrwxrwx 1 root root 11 Apr 24 16:18 s -> /root/try/d  
2
  • the link on ./y/ph is using the full path , i didn't write that to make the test case usable in any folder , but $(pwd) returns the full path to currect directory
    – Bor691
    Apr 24, 2015 at 14:57
  • Don't use ./y/ph instead of full path. Apr 24, 2015 at 16:36
0

You can't do this with symlinks. Symlinks don't work that way. A relative symlink will be relative to the link file itself, not to the path you're in.

You can, however, achieve something similar with a bind mount:

[you@example example]$ mkdir x y x/ph y/ph
[you@example example]$ echo x1 > ./x/file1.txt
[you@example example]$ echo y1 > ./y/file1.txt
[you@example example]$ sudo mount --bind ./x/ph ./y/ph
[you@example example]$ cd ./x/ph
[you@example ph]$ ln -s ../file1.txt
[you@example ph]$ cd ../..
[you@example example]$ cat ./x/ph/file1.txt
x1
[you@example example]$ cat ./y/ph/file1.txt
y1

You must log in to answer this question.

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