[SLUG] [PIG] Removing broken symbolic links, or why IO::All is cool

From: Dylan William Hardison (dylan@hardison.net)
Date: Tue Jul 25 2006 - 00:28:00 EDT


I got tired of 'cleanlinks' (which (used to?) come with X11)
removing empty directories, so here's a script to remove any symlinks
that point to a file that doesn't exist.

Usage: perl delink [dir]

#!/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);
}

-- 
We're here to give you a computer, not a religion.
              -- attributed to Bob Pariseau, at the introduction of the Amiga
-
GPG Fingerprint: E3CD FDAB 82C4 14FD 7B57  430B 770E 0EAF FB53 12C2
-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS).  Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 15:03:18 EDT