http://www.perlmonks.org?node_id=566997

This is a short script using IO::All to remove broken symlinks.

Note, it has to redefine the readlink() method, because as of version 0.35 of IO::All, readlink doesn't handle relative symlinks.

IO::All sure makes my life easier!

#!/usr/bin/perl use strict; use warnings; use IO::All::Link; # Because we redefine readlink()... use IO::All; my $file = shift @ARGV || '.'; my @ios = ( io($file) ); while (my $io = shift @ios) { if ($io->is_dir) { push @ios, $io->all; } elsif ($io->is_link and not $io->readlink->exists) { print "$io is broken, removing.\n"; $io->unlink; } } # this has been submitted as a patch to IO::All... no warnings 'redefine'; sub IO::All::Link::readlink { my $io = shift; my $path = CORE::readlink($io->name); unless (File::Spec->file_name_is_absolute($path)) { $path = File::Spec->join($io->filepath, $path); } IO::All->new($path); }

Replies are listed 'Best First'.
Re: Remove broken symlinks with IO::All
by graff (Chancellor) on Aug 13, 2006 at 03:30 UTC
    Very nice -- thank you, especially for handling relative links. I would just add a couple things, at first glance:

    1. When deleting a stale symlink, the report about the deletion should include the stale target path.

    2. It would be prudent to have a "-n" mode (no deletions, just report the paths and targets of stale links).

    Sometimes, when you detect a stale link, it could be that you would rather just change the target (e.g. because the target was moved to a new disk volume), or it could be that the target is only "temporarily" inaccessible (e.g. because an nfs or samba server is down).

    At the very least, provide enough logging so that it's easy to re-install a deleted link, in case deletion was the wrong thing to do. Better yet, simply report stale links and their targets, and give the user a chance to decide what the appropriate action should be (delete it, replace it, or make sure a given network disk volume comes back online).

    Here's a script I wrote a while back to list all links (good and bad, including link chains:  link.1 -> link.2 -> ... -> data.file). This was done before I knew about IO::All, so it uses unix "find" instead.

      It's mostly a replacement for 'cleanlinks', which comes with Xorg (and Xfree86, too). :)