Search code examples
linuxbashshelladminln

symbolic link without expanding $HOME or "~"?


the basic idea is that I want to link to path that's relative to $HOME, rather than explicitly expand the $HOME variable, as I want to make sure the link works on multiple machines, e.g.,

when I do

ln -s ~/data datalnk

I want it to be directed to directory /home/user/data on one machine which has a user $HOME of /home/user, and to /home/machine/user/data on another machine which has a user $HOME of /home/machine/user/data.

I cannot create a symbolic link on the second machine using

ln -s /home/machine/user /home/user

because I don't have the permission to do that, and I cannot link relative paths as the two machines have different hierarchies of directories.

anyideas on possible ways to fix or circumvent this?

EDIT:

what I am really trying to accompanish is to make the same link work on two macihnes, where the targets have the same directories in terms of their relative path to $/HOME only, not their absolute path, and not their relative path to the link either.


Solution

  • The only way to make symlinks dynamic in this way is to use a relative path instead of an absolute path. In other words, don't start your path with /.

    For example:

    cd
    ln -s data datalnk
    

    At runtime your app or script will need to refer to ~/datalnk or $HOME/datalnk.

    You haven't really said what you're trying to accomplish, so I can't really tell whether I'm solving your problem or suggesting that you need to go at it a different way.