How to Find and Delete Broken Symlinks on Linux


A terminal window on a Linux laptop.
Fatmawati Achmad Zaenuri/Shutterstock

The symbolic inbound links on Linux are a fantastic element, but they can become broken and remaining pointing at practically nothing. Here’s how to locate damaged symbolic one-way links, evaluate them, and get rid of them from your technique if you want to.

Symbolic Inbound links 101

Symbolic back links, also referred to as “soft links” and “symlinks,” are a form of shortcuts that can stage to data files and directories. A symlink looks just like a typical file or directory in a file manager window. It also exhibits up as an entry in a file listing in a terminal window. The file or directory to which the symlink points can be anywhere in the file system tree.

For example, let us say you have a symlink in your property listing named “dave-link” that factors to a file known as “text-file.txt” found somewhere else in the file system tree. Commands you use on the symlink are routinely utilized to the file to which it points. If you attempt to use cat or less on the symlink, you are going to basically see the contents of the “text-file.txt” file.

A normal Linux set up contains several symlinks. Even if you don’t make any on your own, the operating system works by using them. Application set up routines usually use symlinks to issue to executables data files. When the software program is current, the binary file is replaced with the new model, and all the symlinks carry on doing work as before, as long as the new file’s title is the exact as the previous.

We can simply see some symlinks by employing ls in the root listing. Some of the entries are shown in a unique color—on our Ubuntu 20.10 take a look at equipment, they’re displayed in light-weight blue.

We sort the next:

ls /

We can get a deeper glance by using the -l (long listing) selection. We kind the subsequent command to glance at all the “lib” entries and the one “bin” entry:

ls -l /lib* /bin

At the get started of every single line is an “l,” which implies the product is a symlink. The text just after “->” reveals at what the symlink is pointing. In our case in point, the targets are all directories.

The permissions are mentioned as read, write, and execute for the operator, the team, and other individuals. These are default pretend entries. They never replicate the genuine permissions on the objects at which the symlinks place. It’s the permissions on the concentrate on file or directory that just take priority and are honored by the file process.

Broken Symlinks

A symlink is damaged (or still left dangling) when the file at which it details is deleted or moved to one more locale. If an application’s uninstallation regime does not get the job done appropriately, or is interrupted in advance of it completes, you may well be still left with broken symlinks.

If another person manually deletes a file with no understanding symlinks issue to it, those symlinks will no more time function. They’ll be like street indications pointing to a town which is been bulldozed.

We can very easily see this habits making use of a symlink termed “hello” in the current directory. We type the adhering to, using ls to see it:

ls -l

It points to a method identified as “htg” in a directory identified as “bin.” If we “run” the symlink, it executes the software for us:

./howdy

We can now examine if this is what is occurring by functioning the system instantly:

../bin/htg

As envisioned, we get the very same reaction. Let’s delete the method file:

rm ../bin/htg

Now, when we glimpse at the symlink, we see it’s shown in purple simply because Linux understands it is damaged. It also tells us at what it utilised to issue, so we can substitute the file, recompile the method, or do no matter what is essential to restore the symlink.

Note that if we test to run the symlink, the error we get references the symlink identify, rather than the identify of the system to which the symlink factors.

We form the pursuing:

./hello

Finding Damaged Symlinks

Most modern variations of discover have the xtype (prolonged form) choice, which simplifies locating damaged symlinks. We’ll use the l flag with xtype, to explain to it to look for for one-way links. Using discover and xtype as follows, without having any of the other style flags, forces xtype to return broken inbound links:

uncover . -xtype l

Running the command in our check home listing finds fairly a couple damaged symlinks. Note that the look for is recursive by default, so it searches all subdirectories automatically.

The “hello” symlink we broke on objective is mentioned, as we predicted. Just one of the other symlinks is similar to the Firefox browser, and the relaxation are associated with snaps.

If we pipe the output via wc with the -l (lines) solution, we can count the traces, which is the exact same as counting the broken symlinks.

We form the following:

find . -xtype l | wc -l

We’re educated that we have 24 damaged symlinks pointing to nothing at all.

Come across, Evaluation, and then Eliminate

Just before you rush in and delete all broken symlinks, look as a result of the results of the uncover command. See if there’s a valid cause for any of the broken symlinks.

Often, the symlink may be the difficulty, somewhat than the target file. If the symlink was established incorrectly it might level to absolutely nothing, but the serious concentrate on is present. Re-creating the symlink would be the correct in that situation.

It’s also feasible that an evidently broken symlink is becoming made use of as one thing else, this kind of as an indicator of a file lock or other go/no go indicator. Firefox does this that’s what the 1st symlink in our list is. Firefox isn’t utilized on our check equipment, while, so it is secure for us to delete it.

It’s also feasible the target is only present periodically, and this is the envisioned (and sought after) actions of that specific computer software. Probably the concentrate on file is copied from a further device or the cloud, it performs its purpose, and is then deleted yet again, only to be replaced by a diverse software in the next cycle.

The broken symlink could also be a symptom of a software package installation that failed. In that circumstance, instead of deleting the symlink, you should either manually fix it or repeat the set up.

When you have set the broken inbound links you will need to maintain, repeat the command to complete the lookup. The fixed symlinks really should then be absent from the research success.

For safety’s sake, it’s ideal to restrict your symlink removals to your own directories. Be really wary of managing these instructions as root, or on procedure directories.

Eliminating Damaged Symlinks

The -exec (execute) choice runs commands on the uncover lookup success. We’re going to use rm to delete every broken symlink. The string is replaced with the name of each broken symlink as each one is discovered by obtain.

We have to use a semicolon () to terminate the listing of commands we want -exec to run. We’ll use a backslash () to “escape” the semicolon, so it is dealt with as component of the uncover command, somewhat than one thing Bash should really act on.

We style the adhering to:

locate . -xtype l -exec rm  

We’re returned to the command prompt with no sign that something has happened. To validate the broken one-way links have been removed, we repeat the command to seem for them, as follows:

find . -xtype l

There aren’t any matching benefits, which indicates the broken symlinks have been eradicated.

Don’t forget to Overview Very first

Again, constantly choose the time to assessment a listing of symlinks ahead of you run the command to delete them. You can prevent deleting any you’re uncertain about by jogging the command to delete them in the proper directories.

For case in point, earlier mentioned, we could have operate the command in the “.snap” listing, and then manually eliminated the solitary “hello” symlink. This would have remaining the Firefox lock symlink untouched.

Exit mobile version