ln command.To create a symlink to a file, folder (directory), to another symlink, or even to a filepath that doesn't exist, execute the following terminal command [how?]:
ln -s target new-symlink
Warning: the -s flag (or --symbolic) is important. Without it, ln creates a hard link instead of a symbolic link.
The command above will create a new symlink in the current working directory called new-symlink that points to filepath target. See [How to Refer to Files and Folders using the Terminal on Linux Mint] for how to write this filename argument.
Creating Relative Symlinks
A symlink may symlink to a relative filepath or to an absolute filepath. The relative filepath is relative to the location of the symlink itself, not to the location of the working directory. For example: ($ below represents the shell prompt)
$ echo "Hello world!" > foo
$ ln -s foo bar
$ cat bar
Hello world!
$ mkdir fries
$ mv bar fries
$ ls --file-type fries
bar@
$ cat fries/bar
cat: fries/bar: No such file or directory
Above, we create a symlink bar that points to foo. This is a relative filepath, meaning that bar always points to a foo that exists in the same directory as bar.
You can see we put the text "Hello world! in foo, and we're able to get the same text printed in the terminal by using the cat command on bar, meaning the symlink is working.
Then we make a directory calling fries and move our symlink to inside of it. Now bar exists in fries/bar. We haven't moved foo to fries, so there is no fries/foo currently. When we try to access fries/bar using cat, the symlinked filepath resolves to fries/foo, which doesn't exist.
The operating system performs the symlink resolution transparently. cat doesn't notice that fries/bar is a symlink that exists, and what really doesn't exist is the symlinked filepath fries/foo, because from its perspective it's trying to access fries/bar and the system tells it that fries/bar isn't a file. That's why cat reports a confusing error saying that fries/bar doesn't exist, even though we already confirmed its existence with ls.
In general, when a program says a "file" or "directory" doesn't exist, it isn't considering that the filepath may be symlinked to different filepath. It's possible for a program to check if the filepath is a symlink, and to check if it's a valid symlink or a "dangling" symlink to an non-existing file or folder, but that's normally not done, since every single filepath a program accesses can be a symlink.
Relative symlinks may refer to a files in parent directories by including ../ in the filepath, e.g. ../sibling-folder/file.
Creating Absolute Symlinks
A symlink may alternatively use an absolutely filepath, which starts with a forward slash (/). For example:
ln -s /bin the-bin
Above, the symlink the-bin will always point to /bin even if we move the symlink to another location using mv.
Observation: in some cases, a symlink to an absolute filepath doesn't work for security reasons, e.g. Apache may not follow absolute symlinks in certain configurations even though relative symlinks will still work.