I have been able to find a command to get a list of the broken links only, but not the "working" symbolic links
find -H mydir -type l
How can I do the opposite?
If I have these files in mydir
:
foo
bar
bob
carol
If they are all symbolic links, but bob
points to a file that doesn't exist, I want to see
foo
bar
carol
Use the ls -l command to check whether a given file is a symbolic link, and to find the file or directory that symbolic link point to. The first character “l”, indicates that the file is a symlink. The “->” symbol shows the file the symlink points to.
If you want to list all symlinks down one level in the current directory, use maxdepth flag like below. This will recursively list all the symlinks in the current directory. And also, it shows the actual files it points to. For more details, refer man pages.
3. Finding Broken Symlinks. The -H, -L and -P options control how symbolic links are treated, and when omitted, use -P as the default. When -P is used and find examines or prints information from a symbolic link, the details are taken from the properties of the symbolic link itself.
A symlink is broken (or left dangling) when the file at which it points is deleted or moved to another location. If an application's uninstallation routine doesn't work properly, or is interrupted before it completes, you might be left with broken symlinks.
With find(1)
use
$ find . -type l -not -xtype l
This command finds links that stop being links after following - that is, unbroken links. (Note that they may point to ordinary or special files, directories or other. Use -xtype f
instead of -not -xtype l
to find only links pointing to ordinary files.)
$ find . -type l -not -xtype l -ls
reports where they point to.
If you frequently encounter similar questions in your interactive shell usage, zsh is your friend:
% echo **/*(@^-@)
which follows the same idea: Glob qualifier @
restricts to links, and ^-@
means "not (^
) a link (@
) when following is enabled (-
).
(See also this post about finding broken links in python, actually this demonstrates what you can do in all languages under Linux. See this blog post for a complete answer to the related question of "how to find broken links".)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With