Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Find out where symlinks point

by merlyn (Sage)
on Aug 09, 2000 at 15:31 UTC ( #27007=sourcecode: print w/ replies, xml ) Need Help??

Category: Utility Scripts
Author/Contact Info Randal L. Schwartz, aka merlyn
Description: Walks through one or more directories specified on command line, and fully expands any symbolic links within those directories to their real locations, taking into consideration all the relative and absolute symlinks that occur, recursively. Originally written for a Performance Computing/SysAdmin magazine Perl column. Go see there for a line by line description.
#!/usr/bin/perl -w
use strict;
$|++;

use File::Find;
use Cwd;

my $dir = cwd;

find sub {
  return unless -l;

  my @right = split /\//, $File::Find::name;

  my @left = do {
    @right && ($right[0] eq "") ?
      shift @right :            # quick way
        split /\//, $dir;
  };    # first element always null

  while (@right) {
    my $item = shift @right;
    next if $item eq "." or $item eq "";

    if ($item eq "..") {
      pop @left if @left > 1;
      next;
    }

    my $link = readlink (join "/", @left, $item);

    if (defined $link) {
      my @parts = split /\//, $link;
      if (@parts && ($parts[0] eq "")) { # absolute
        @left = shift @parts;   # quick way
      }
      unshift @right, @parts;
      next;
    } else {
      push @left, $item;
      next;
    }
  }
  print "$File::Find::name is ", join("/", @left), "\n";

}, @ARGV;

Comment on Find out where symlinks point
Download Code

Back to Code Catacombs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: sourcecode [id://27007]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-09-19 22:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (150 votes), past polls