Different file systems in the UNIX/Linux universe allow a variety of entries such as regular files, directories, sockets, named pipes, and links. In this article I will explain to you what links are, which types of links exist, how to create a symbolic link, and how to detect broken symbolic links easily. Links allow you to have multiple entry points into a file-system, allowing you to have many less copies (sometimes only one) yet maintaining flexibility by having multiple ways to reach a given file.
On the UNIX/Linux command line, the tool
ln abbreviates the term link. It allows you to create an additional reference to a file, or directory. It does that by adding an additional name of an entry in the file allocation table of the file system. Having done that you can access the referenced file or directory with both the original name, and the new name, too. Think of a single person might be an employee, client and prospect.
ln belongs to the list of essential software in Unix/Linux. On Debian GNU/Linux and Ubuntu it is part of the package
Hardlinks vs Softlinks
There is a clear distinction between hardlinks, and softlinks. Softlinks are named symbolic links, too. There are quite a few differences between the two types as listed below:
|Hard Links||Soft link (symbolic link)|
|Target must exist||Target may already exist, but does not have to|
|Allowed within file systems only||Allowed between different file systems|
|Links directly to the place the file is stored||Links to the entry in the file system table (node)|
|Removing the link means removing the whole file||Removing the link means removing the link to the node, not the file itself|
How to Create Symbolic Links
ln command without options creates a hardlink from source to destination. With the help of the option
-s it creates a symbolic link, instead.
-s is the short name of the option, whereas
--symbolic is the longer name. The link will be created in the current directory of the filesystem at the moment it is created. Developers just use the term symlink. In general, call it as follows:
$ ln -s [target] [link_name]
target is the file or directory to link to, and
link_name is the name of the link that shall reference to the target. As an example, this call creates a symbolic link from
Example 1: Creating a symbolic link
$ ln -s /usr/bin/python link_to_python $ ls -la link_to_python lrwxrwxrwx 1 frank frank 15 Oct 5 14:25 link_to_python -> /usr/bin/python
To see what
ln does, a useful option is
--verbose for the longer option). This gives you further information on the action.
Example 2: Creating a symbolic link with additional information
$ ln -sv /usr/bin/python link_to_python link_to_python -> /usr/bin/python
As you can see, the output of the command depicts the new link created.
First, a regular use case you may not have been aware of are two commonly used file system entries -
. (the local directory) and
.. (parent directory). These are implemented as symbolic links.
Second, developers install several versions of a program to do their tests. The usage of symbolic links can become quite handy to reference to the current version that is tested, and keeps two or more instances of it available without much trouble.
Finding Dead Symbolic Links
What if you delete a file that has a symbolic link pointing to it? We refer to these links with a non-existent "target" as "dead" links. To detect such cases the find command is quite helpful. Therefor, the option
-L, and the action
-type l comes into play. The
-L flag tells
find to follow symbolic links, while
-type l will only match symbolic links.
In Example 3 the current directory contains two files named
backup, as well as two symbolic links -
data pointing to
generic pointing to
files. Unfortunately, the
files file/directory does not exist.
Free eBook: Git Essentials
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it!
find command, we can examine the links and outputs their names in case the link target does not exist. In our case it is the symbolic link named
generic pointing to
files that does not exist.
Example 3: Detecting broken links using
$ ls -la total 4 drwxr-xr-x 2 frank frank 1024 Oct 5 13:36 . drwxrwxrwt 44 root root 3072 Oct 5 13:35 .. -rw-r--r-- 1 frank frank 0 Oct 5 13:34 auto -rw-r--r-- 1 frank frank 0 Oct 5 13:34 backup lrwxrwxrwx 1 frank frank 1 Oct 5 13:35 data -> auto lrwxrwxrwx 1 frank frank 1 Oct 5 13:35 generic -> files $ find -L . -type l ./generic
As an alternative, you may like the
symlinks command. This is a non-default package available for both Debian GNU/Linux, and Ubuntu. With the two options
symlinks detects the entries. As seen in Example 4 below, links without valid targets are classified as dangling.
Example 4: Detecting broken links using
$ symlinks -sv . dangling: /directory/generic -> files relative: /directory/data -> auto
The concept of symbolic links are no big mystery. Creating these references can simplify the use of software, and helps to test different versions of it on the same system. Keep in mind that the Linux Professional Institute (LPI) will ask you questions about it during certification for LPI1.
The author would like to thank Gerold Rupprecht for his support, and critics while preparing this article.