Linux – Is it possible to create symbolic links from scripts?

Is it possible to create symbolic links from scripts?… here is a solution to the problem.

Is it possible to create symbolic links from scripts?

This question follows: Symlinks not working when link is made in another directory?

Let’s say I have a directory that contains projects, and a script that creates symbolic links from one part of the project directory to another. Is it possible that these symbolic links are relative (rather than absolute), even if the location of the script is somewhere else entirely?

I’m interested in doing this so that the project directory can be moved without breaking the absolute link in it.

Solution

There is nothing special about running commands from a script. You just do it.

$ mkdir originals
$ echo "weird" > originals/original.txt
$ mkdir copies
$ ln -s .. /originals/original.txt copies/copy.txt
$ cat copies/copy.txt
weird

Note that you do not have to change the working directory before creating the relevant links. The symbolic link will be relative to the link, not to the current directory.

Related Problems and Solutions