How to find all the broken symbolic links on Ubuntu. On Linux find bad symbolic links and remove broken symlinks. Users can easily identify broken symlinks using find command. It can be used to list all the broken symbolic links on Linux Ubuntu System.
symlink
symlink() creates a symbolic link named linkpath which contains the string target. Symbolic links are interpreted at run time as if the contents of the link had been substituted into the path being followed to find a file or directory.
Symbolic links may contain .. path components, which (if used at the start of the link) refer to the parent directories of that in which the link resides. A symbolic link (also known as a soft link) may point to an existing file or to a nonexistent one; the latter case is known as a dangling link.
The permissions of a symbolic link are irrelevant; the ownership is ignored when following the link, but is checked when removal or renaming of the link is requested and the link is in a directory with the sticky bit (S_ISVTX) set.
Using find command
Ubuntu users can use find command to list all the broken symbolic links:
find . -xtype l
The find command is sued to search for files in a directory hierarchy. In this context {finding broken links}, the following command arguments is used:
1. -L Follow symbolic links – When find examines or prints information about files, the information used shall be taken from the properties of the file to which the link points, not from the link itself (unless it is a broken symbolic link or find is unable to examine thefile to which the link points). Use of this option implies -noleaf.
If you later use the -P option, -noleaf will still be in effect. If -L is in effect and find discovers a symbolic link to a subdirectory during its search, the subdirectory pointed to by the symbolic link will be searched. When the -L option is in effect, the -type predicate will always match against the type of the file that a symbolic link points to rather than the link itself (unless the symbolic link is broken). Actions that can cause symbolic links to become broken while find is executing (for example -delete) can give rise to confusing behaviour. Using -L causes the -lname and -ilname predicates always to return false.
2. -xtype c – The same as -type unless the file is a symbolic link. For symbolic links: if the -H or -P option was specified, true if the file is a link to a file of type c; if the -L option has been given, true if c is `l’. In other words, for symbolic links, -xtype checks the type of the file that -type does not check.
3. -type c – File is of type c. Where c is l or symbolic link; this is never true if the -L option or the -follow option is in effect, unless the symbolic link is broken. If you want to search for symbolic links when -L is in effect, use -xtype.
Using symlinks command
Before using the symlinks, you will have to install it manually:
sudo add-apt-repository universe
sudo apt install symlinks
symlinks: scan/change symbolic links. The usage command is “symlinks [-cdorstv] dirlist”. The command uses the following flags:
1. -c == change absolute/messy links to relative
2. -d == delete dangling links
3. -o == warn about links across file systems
4. -r == recurse into subdirs
5. -s == shorten lengthy links (displayed in output only when -c not specified)
6. -t == show what would be done by -c
7. -v == verbose (show all symlinks)
Once isntalled, run the command as:
symlinks -dr
OR
symlinks -r directory
OR
symlinks -d .