How to Manage Symlinks in Linux

Symlinks Featured Image

Symlinks are files that link directly to other files. In Linux systems, symlinks serve as shortcuts.

The term “symlink” is a portmanteau of “symbolic” and “link,” highlighting the utility of such files as symbolic references to other things.

So-called “hard” links function more as copies of the files they reference than literal links. “Soft” or symbolic links simply point at their targets. Deleting these links does nothing to the files they point to, and as many symlinks as are needed can be created for convenience. This makes them great for pointing to files across filesystems and partitions alike.

Uses of Symlinks

Changing the underlying structure of a filesystem for a single application to run more efficiently would be a serious chore. Instead, symlinks are often used to make matters simpler and create artificial file hierarchies for programs to reference without affecting the locations of the original files.

The use of symlinks makes accommodating different programs easier, but it also complicates the analysis of the file system.

Issues with Symlinks

When symlinks are working properly, they spell out a clear path to a file that actually exists. Failing symbolic links, however, point to files that do not exist or have been deleted. These symbolic links create confusion for both human users and programs that depend on their accuracy.

If files targeted by symlinks are swapped out, the link itself goes on pointing at the new file, disregarding its contents entirely. This blind faith functionality makes link chaining and relative links a possibility.

Link chaining, in particular, can lead to cyclic links (link loops of infinite length) if a link references a second link that, in turn, points back to the first.

Such conundrums are not necessarily brought about by human intervention alone; inoptimal device mounting standards and certain automated processes can contribute to creating what are termed as “dead links” in unfortunate abundance. This is precisely where management techniques come in handy.

Managing Symlinks

In Linux systems there are a variety of utilities made readily available for handling symlinks. A default inclusion in coreutils is ln, which facilitates the creation of such links from the terminal.

To fully manage symbolic links, however, you will need to be able to find them and analyze them quickly. A simple command-line option worth considering for this purpose is aptly named symlinks.

Although some Linux distros such as Fedora come with this tool installed by default, others, such as Ubuntu, do not. To install “Symlinks” in Ubuntu, simply open a terminal window and enter the following:

sudo apt install symlinks
Symlinks Apt Install

Creating Symlinks

Creating symlinks from your terminal is easy in Linux. Enter the code that follows, changing “original-file.txt” to the name and file extension of your chosen target, then alter “linkname” to whatever you want it to be.

ln -s original-file.txt linkname
Symlinks Ln S 2 Hecho

The ln utility is for making links, and it will do so when you run it. The -s included in the command above makes the generated link symbolic.

Relative symbolic links can also be created by adding an -r to the same command as follows:

ln -rs original-file.txt linkname
Symlinks Ln Rs 2 Hecho

Relative links remain functional regardless of changes in mount points.

Finding Symlinks

The Symlinks utility mentioned above provides us with a simple way of finding symbolic links in a given directory. The command for this is as follows: (Change “directory-name” to the full path to the directory you want to search.)

symlinks -v directory-name
Symlinks V 2 Hecho

Adding an r to this command tells Symlinks to recursively check files within the specified directory. This looks like the following:

symlinks -rv directory-name
Symlinks Rv 2 Hecho

Be careful using recursion if you are worried about problems arising with cyclic links. Cyclic links are links that end up erroneously looping back to themselves; they can cause the Symlinks utility to hang as it attempts to recurse into their infinite structure.

The non-recursive version of the command shown above will simply reveal any existing cyclic links to be “dangling,” or, in other words, broken. It is the analysis that the Symlinks tool is capable of that allows it to actually fix such broken links.

Fixing Symlinks

Fixing symbolic links in a given directory is relatively straightforward with Symlinks. Here’s the command to use:

symlinks -cds directory-name
Symlinks Cds 2 Hecho

The command above does multiple things at once. It converts any absolute links it finds to relative links, removes dangling links and shortens “lengthy” links (links with a lot of “../” in their paths).

If you are unsure of the potential results of running this operation, you can test what -c alone would do without changing anything by running the following:

symlinks -t directory-name
Symlinks T 2 Hecho

Hopefully, you now have a better understanding of what symbolic links are and how you can manage them efficiently. Check out the rest of the Symlinks utility’s capabilities to run more specific operations on your filesystem.

Subscribe to our newsletter!

Our latest tutorials delivered straight to your inbox