What is Symbolic link?

A special file whose contents are the path of another file or directory; opening it uses whatever that path leads to. It is created with ln -s target linkname, and ls -l shows it with a leading l as "linkname -> target". It can point to other file systems and to directories, but if the target is deleted or moved, it becomes a broken link with nothing to follow. It is distinguished from a hard link, which is another name for the same inode.

Japanese name: シンボリックリンク

Symbolic link appears in 1 practice question across 1 topic, including Linux and Command-Line Basics.

Field: Linux and Command-Line Basics

Creating one and how it looks

If created as ln -s /opt/tools/sim-2.3 /opt/tools/sim, opening or cd-ing into /opt/tools/sim actually uses /opt/tools/sim-2.3. ls -l shows "sim -> /opt/tools/sim-2.3", and readlink or realpath shows where it leads. The permissions of the link itself are not used; access is decided by the permissions of the file it leads to.

Relative and absolute paths

If the link contains a relative path, it is followed starting from the directory where the link is placed. This has the advantage that the relationship is kept when a whole directory is moved elsewhere, but moving only the link breaks it. With an absolute path, the link points to the same place wherever it is moved, but changing the layout of the target directories breaks it.

Difference from a hard link

A hard link adds another name pointing to the same inode, and the contents remain even if the original name is deleted. However, it can be created only within the same file system and normally not for directories. A symbolic link only records a path, so it can cross file systems and point to directories, but it breaks when the target disappears.

FAQ

Where are symbolic links used?

A well-known use is switching tool versions. If /opt/tools/sim points to the directory of the latest version, users' scripts keep the same path and the version can be changed just by re-pointing the link. They are also used for naming shared libraries, from libfoo.so to libfoo.so.1.2, and to refer from inside a project to large data kept on another disk.

How can I find broken links?

If the target shown by ls -l does not exist, many environments mark it by color or display. With GNU find, find . -xtype l lists symbolic links that lead nowhere. Trying to open a broken link gives a "No such file or directory" error.

Symbolic link in the explanations

Questions that mention Symbolic link (1)

  1. When the commands above are run in order, which of the following is the result of the last two cat commands?

Books on Linux and Command-Line Basics Ad

As an Amazon Associate I earn from qualifying purchases.

Related terms

Hard link inode cron Environment variable File permissions Git grep I/O redirection Pipe (|) Regular expression

Back to the glossary