Symbolic link without extension $ HOME or "~"?

The main idea is that I want to bind to the path related to $ HOME, instead of explicitly expanding the $ HOME variable, because I want to make sure that the link works on several machines, for example,

when i do

ln -s ~/data datalnk 

I want it to be directed to the /home/user/data directory on the same computer as user $HOME from /home/user to /home/machine/user/data on another computer that has user $HOME /home/machine/user/data .

I cannot create a symbolic link on a second computer using

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

because I don’t have permission to do this, and I can’t link relative paths, because the two machines have different directory hierarchies.

anyideas about possible ways to fix or work around this?

EDIT:

what I'm really trying to accompany is to make the same connection with two engines, where the targets have the same directories in terms of their relative path only to $ / HOME, and not to their absolute path, not to their relative path to the link.

+7
source share
3 answers

The only way to make symbolic links this way is to use a relative path instead of an absolute path. In other words, do not start your journey with / .

For example:

 cd ln -s data datalnk 

At run time, your application or script will need to reference ~/datalnk or $HOME/datalnk .

You really did not say what you are trying to accomplish, so I cannot say if I am solving your problem or suggesting that you need to go differently.

+1
source

tl, dr it will not work

You can use an escaping mechanism such as single quotes to get ~ into a symlink:

 > cd ~ > echo hello > a > ln -s '~/a' b 

However, ~ is a shell extension and is not understood by the file system (in fact, for the file system it is “just a different character”). This is good - do you need the file system level to know about environment variables since ~ usually defined by $HOME ?

 > ls -lb lrwxrwxrwx 1 root root 3 Oct 27 17:39 b -> ~/a > ls b ls: b: No such file or directory 

You can still “manually” look at symbolic link entries (as done with ls -l ), but this should be done in an opaque way with the program (think of “.LNK” on Windows). As you can see, the file system just does not understand ~ .

Happy sh'ing.

+7
source

First of all: this cannot be done directly. Symbolic links are text files, extensions are not executed. If you cannot formulate a fixed relative or absolute path to the place you are accessing, you cannot symbolically refer to it.

You can create a script to put the links in the appropriate directories in the appropriate places, but the best way depends on your application.

+1
source

All Articles