A symbolic link or symlink is a special type of file that contains a reference to another file or directory as an absolute or relative path. For most operations, symbolic links are transparent and the read or write operations on symbolic link files are redirected to the files that symbolic link files link to.

A symbolic link, or symlink, is a special type of file whose function is to link to another file, which may be of any type: a regular file, a directory, another symlink, etc. Reading from or writing to a symlink will access the file it represents. This makes the symlink appear to have no content of its own, but in reality it contains the text of the path to the file to which it links.

A symlink does not depend on the target file and continues to exist even if the target is moved or deleted. Moving or removing the target does not affect the symbolic link file; it will still have the same path although the path will now be invalid.

In the default ls colors, the filename of a symlink is shown in turquoise letters (the name of a broken symlink, one whose target path is no longer valid, is shown in red letters). A symlink can also be identified in the output of ls -l by the arrow after its filename pointing to the path of the file it links to.

lrwxrwxrwx 1 root root  22 Oct 23 12:43 telegram -> /opt/telegram/Telegram

The first letter l of the metadata string lrwxrwxrwx also identifies the file as a symlink. The letters rwxrwxrwx are the "dummy permissions" of the symlink. All symlinks have the same apparent permissions, but their real permissions are those of the file they link to. A user who does not have permission to read a file is not able to read the file by reading a symlink to it (thankfully).

The command to create a symlink is:

ln -s source destination

In this command, source is the filename or path that you want the symlink to point to, and destination is the name of the symbolic link created.

For example, if your current directory contains a file named test.sh, you can create a symlink to it using the command:

ln -s test.sh testlink

A new file named testlink will be created in the current directory and it will link to test.sh.

You can also use absolute paths. For example, using the absolute path to test.sh and creating testlink in the current directory:

ln -s /path/to/test.sh testlink

Using relative paths, which are relative to the directory that contains the symlink, may make the symlink more fragile. If a symbolic link holding a relative path is moved into another directory, the symlink will become invalid, unless its target was moved along with it. If you move a symbolic link that you created with an absolute path, the link will still be valid, as long as the target file has not also moved.

Symbolic links can also be created in graphical file browsers. For example, in some versions of Ubuntu, a contextual menu in a folder window or on the desktop may offer an option like Create Shortcut, which makes a symlink.

Related tags: