#22 Checking Symbolic Links

Symbolic links are nice, but they can be a real pain when they get broken. This script checks a directory tree for symbolic links and makes sure they are good.

The Code

  1 #!/usr/bin/perl
  2 use strict;
  3 use warnings;
  4
  5 use File::Find ();
  6
  7 use vars qw/*name *dir *prune/;
  8 *name   = *File::Find::name;
  9 *dir    = *File::Find::dir;
 10 *prune  = *File::Find::prune;
 11
 12 # Traverse desired filesystems
 13 File::Find::find({wanted => \&wanted}, @ARGV);
 14 exit;
 15
 16
 17 sub wanted {
 18     if (-l $_) {
 19         my @stat = stat($_);
 20         if ($#stat == -1) {
 21             print "Bad link: $name\n";
 22         }
 23     }
 24 }
 25

Running the Script

The script takes a directory or set of directories as arguments. It then scans each directory tree and reports ...

Get Wicked Cool Perl Scripts now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.