How to create a relative symlink which works across PC

symlink

I have the following directory structure to start with

~/a/bin
~/a/src -> /home/user/b/sync/src

I created a symlink

cd ~/a/src
ln -s ../bin bin-xyz

now bin-xyz is a broken link, and there is no way of cd-ing into it.

What i am looking for?

How to create a symlink inside a symlinked folder such that the relative path is not broken. For example,

 cd ~/a/src/bin-xyz 

or

 cd ~/b/sync/src/bin-xyz

should work provided that there is bin directory in ~/a/ or ~b/sync/

Why i am looking for this ?

I am syncing ~/b/sync/src across machines, where ~/a/ is my workspace in both the machines but the user name of the machines differ. So, having a relative path that works means an easier work-flow for me.

I am using bittorent sync for syncing.

Thanks 🙂

Best Answer

now bin-xyz is a broken link

The reason for that is hopefully obvious: Either .. has to refer to the actual parent directory or else it would be completely ambiguous.

Since ~/a/src -> /home/user/b/sync/src, I presume with that as $PWD and ln -s ../bin bin-xyz you're trying to link /home/user/b/sync/src/bin-xyz to /home/user/a/sync/bin. In which case you should just use absolute paths:

 ln -s /home/user/a/sync/bin /home/user/b/sync/src/bin-xyz

The reason ln doesn't fill in the .. blanks in this regard is that it, unlike your current shell, has not been tracking your movement and has no way to know that by .. you meant something other than the real parent directory.

It's (maybe?) implied in your question that you can't do this because there are actually two different systems involved. That implies either a network share is mounted or you use the same block device with both, implying in turn an XY problem -- can't solve that without more details.