How To Discover Damaged Symlinks And Delete Them On Linux
Some time in the past, we defined what’s Mushy hyperlink and Arduous hyperlink with sensible examples. At instances you would possibly neglect to delete the unused tender hyperlinks or symlinks that don’t level anyplace. Protecting unused hyperlinks on the filesystem doesn’t hurt the system, besides occupying a bit of little bit of disk house. So there isn’t a level of maintaining them. Simply discover the damaged symlinks and delete them in your Linux system.
Discover Damaged Symlinks And Delete Them On Linux
There could possibly be some ways to seek out damaged symlinks or tender hyperlinks in Linux. Right here I’ve given two strategies.
1. Discover and delete damaged tender hyperlinks utilizing “Symlinks” utility
Symlinks is a command line symbolic hyperlink upkeep utility. It scans the given listing and lists the symbolic hyperlinks if there are any.
Symlinks is offered on the official repositories of many Linux distributions.
To put in Symlinks on CentOS, run:
$ sudo yum set up symlinks
$ sudo dnf set up symlinks
On Debian, Ubuntu, Linux Mint:
$ sudo apt-get set up symlinks
On Ubuntu , that you must allow the [universe] repository.
$ sudo add-apt-repository universe
After putting in Symlinks, run the next command to seek out and delete damaged symbolic hyperlinks:
$ symlinks -dr
The above command will scan for damaged symbolic hyperlinks within the given listing and delete them.
d – delete dangling symbolic hyperlinks
r – recursive
Let me present you an instance. I’m going to create a file named “abc” within the present listing.
$ contact abc
Subsequent I create a symlink known as ‘xyz’ for ac file:
$ ln -s abc xyz
Now delete the precise file i.e. abc.
$ rm abc
As you might know already, as soon as we deleted the precise file, the symlink has no worth. As a result of it’s simply shortcut and is damaged now.
To seek out the damaged symlinks, run:
$ symlinks .
Please observe the dot (.) on the finish. It implies that we’re looking for damaged hyperlinks within the present listing.
dangling: /residence/sk/xyz -> abc
Right here xyz is dangling as a result of we deleted the precise supply file “abc”. So allow us to delete this damaged hyperlink utilizing command:
$ symlinks -dr .
dangling: /residence/sk/xyz -> abc
deleted: /residence/sk/xyz -> abc
Lastly run ‘symlinks’ command as soon as once more to ensure if there are any leftover damaged hyperlinks.
2. Discover and delete damaged tender hyperlinks utilizing “discover” command
We will additionally use ‘discover’ command to delete damaged symlinks.
To seek out damaged symlinks, however not delete them, run:
$ discover . -xtype l
This command will lists the damaged symlinks within the present listing. If you wish to seek for damaged symlinks on a selected path, for instance /usr/native/, do:
$ discover /usr/native/ -xtype l
The above command will seek for damaged hyperlinks in /usr/native/ and its sub-directories. As you possibly can see within the above output, there are a lot of damaged symlinks.
Alternatively, use the next command to checklist the unused symlinks:
$ discover /usr/native/ -xtype l ! -exec check -e ; -print
It is usually potential to verify the place the damaged hyperlinks level. It’s a good follow to be sure that we’re deleting the supposed symlinks.
$ discover /usr/native/ -xtype l -exec ls -l +
lrwxrwxrwx 1 root root 28 Jan 21 08:53 /usr/native/bin/drracket -> /residence/sk/racket/bin/drracket
lrwxrwxrwx 1 root root 27 Jan 21 08:53 /usr/native/bin/gracket -> /residence/sk/racket/bin/gracket
lrwxrwxrwx 1 root root 32 Jan 21 08:53 /usr/native/bin/gracket-text -> /residence/sk/racket/bin/gracket-text
lrwxrwxrwx 1 root root 24 Jan 21 08:53 /usr/native/bin/mred -> /residence/sk/racket/bin/mred
lrwxrwxrwx 1 root root 29 Jan 21 08:53 /usr/native/bin/mred-text -> /residence/sk/racket/bin/mred-text
lrwxrwxrwx 1 root root 23 Jan 21 08:53 /usr/native/bin/mzc -> /residence/sk/racket/bin/mzc
lrwxrwxrwx 1 root root 24 Jan 21 08:53 /usr/native/bin/mzpp -> /residence/sk/racket/bin/mzpp
lrwxrwxrwx 1 root root 28 Jan 21 08:53 /usr/native/bin/mzscheme -> /residence/sk/racket/bin/mzscheme
lrwxrwxrwx 1 root root 26 Jan 21 08:53 /usr/native/bin/mztext -> /residence/sk/racket/bin/mztext
lrwxrwxrwx 1 root root 30 Jan 21 08:53 /usr/native/bin/pdf-slatex -> /residence/sk/racket/bin/pdf-slatex
lrwxrwxrwx 1 root root 29 Jan 21 08:53 /usr/native/bin/plt-games -> /residence/sk/racket/bin/plt-games
lrwxrwxrwx 1 root root 28 Jan 21 08:53 /usr/native/bin/plt-help -> /residence/sk/racket/bin/plt-help
lrwxrwxrwx 1 root root 28 Jan 21 08:53 /usr/native/bin/plt-r5rs -> /residence/sk/racket/bin/plt-r5rs
lrwxrwxrwx 1 root root 28 Jan 21 08:53 /usr/native/bin/plt-r6rs -> /residence/sk/racket/bin/plt-r6rs
lrwxrwxrwx 1 root root 34 Jan 21 08:53 /usr/native/bin/plt-web-server -> /residence/sk/racket/bin/plt-web-server
lrwxrwxrwx 1 root root 26 Jan 21 08:53 /usr/native/bin/racket -> /residence/sk/racket/bin/racket
lrwxrwxrwx 1 root root 24 Jan 21 08:53 /usr/native/bin/raco -> /residence/sk/racket/bin/raco
lrwxrwxrwx 1 root root 28 Jan 21 08:53 /usr/native/bin/scribble -> /residence/sk/racket/bin/scribble
lrwxrwxrwx 1 root root 29 Jan 21 08:53 /usr/native/bin/setup-plt -> /residence/sk/racket/bin/setup-plt
lrwxrwxrwx 1 root root 26 Jan 21 08:53 /usr/native/bin/slatex -> /residence/sk/racket/bin/slatex
lrwxrwxrwx 1 root root 29 Jan 21 08:53 /usr/native/bin/slideshow -> /residence/sk/racket/bin/slideshow
lrwxrwxrwx 1 root root 27 Jan 21 08:53 /usr/native/bin/swindle -> /residence/sk/racket/bin/swindle
To delete all the damaged hyperlinks directly, run:
$ sudo discover /usr/native/ -xtype l -delete
$ sudo discover /usr/native/ -xtype l ! -exec check -e ; -delete
Substitute /usr/native/ with your personal path.
Hope it helps.
Thanks for stopping by!
Assist us that can assist you:
Have a Good day!!