Table of Contents
This tip is about the how to Find/Delete Broken Symlinks in Linux. So read this free guide, How to Find/Delete Broken Symlinks in Linux step by step. If you have query related to same article you may contact us.
How to Find/Delete Broken Symlinks in Linux – Guide
On Linux systems, symlinks are quite valuable. They can help you remember where critical files are stored on a system, simplify access to those files, and save a lot of disk space and time by eliminating the need to duplicate large files just to make them more accessible.
Symbolic links, often known as “symlinks” or “soft links”, are very small files. In fact, all a symbolic link includes is the name of the file it links to, along with the file system path in most cases (relative to the current or absolute location). Even if a file named ref1 leads to a file named /apps/refs/ref-2020 that is 2 terabytes in size, ref1 will be 19 characters long. It will only be 10 characters long if it is linked to./ref-2020. Only eight if referring to ref-2020.
If you type “vi ref1” (where ref1 is the name of a symbolic link), you will end up up changing the content of the links to the ref1 file, not the symlink itself. Linux systems understand symlinks and always do the right thing. You will see the contents of the linked file if you use commands like cat, more, head or tail.
When you delete a symbolic link, however, you are only deleting the link, not the referenced file. Linux, once again, does what is logical. Symbolic links were created for ease of use and file sharing – nothing more.
Finding broken symlinks
The find command has an option that allows you to find symbolic links that point to files that no longer exist. This command lists symbolic links in the current directory:
$ find. -type l
The “l” (lowercase L) tells the find command to look for symlinks.
The command shown below, on the other hand, searches the current directory for symbolic links that point to files that do not exist:
$ find. -xtype l
To avoid errors when the command tries to look in files or directories that you don’t have permission to examine, you can send all error output to /dev/null like this:
$ find. -xtype l 2>/dev/null
You can also find broken symlinks with a command like this. It’s longer than the previous one, but it should do the same thing:
$ find. -type l! -exec test -e {} ; -print 2>/dev/null
What to do with broken symlinks
Unless you know that the file referenced by a symbolic link will be overwritten, the best move is to simply remove the broken link. In fact, you can find and remove broken symlinks in a single command if you like, with a command like this:
$ find. -xtype l 2>/dev/null -exec rm {} ;
The rm {} part of this command becomes a “remove filename” command.
If you instead want to associate the symlink with a different file, you will have to remove the symlink and recreate it so that it points to the new file. Here’s an example:
$ rm ref1 $ ln -s /apps/data/newfile ref1
Final note
I hope you like the guide How to Find/Delete Broken Symlinks in Linux. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends.