Dereferencing links
Kevin D. Clark
clark_k at pannaway.com
Fri Apr 8 11:15:01 EDT 2005
Jim Kuzdrall writes:
> Is there a command line function to collapse a group of symbolic
> links, replacing them with the files they reference?
Older editions of the _Programming Perl_ book come with the program
"sl" (for "show links"). Either this does what you want or else it
should be easy to modify this to get what you want.
$ sl /dev/mouse
/dev/mouse:
/dev/mouse -> input/mice
input/mice
Regards,
--kevin
--
GnuPG ID: B280F24E And the madness of the crowd
alumni.unh.edu!kdc Is an epileptic fit
-- Tom Waits
-------------- next part --------------
#!/usr/bin/perl
die "Usage: sl [filenames]\n" unless @ARGV;
# Preliminaries.
$| = 1;
chop($cwd = `pwd`) || die "Can't find current directory: $!\n"
if @ARGV > 1;
print "\n";
# Do each name.
foreach $name (@ARGV) {
@indent = ();
print "$name:\n";
@path = split(m;/;, $name);
# Make an absolute path relative to /.
if (@path && $path[0] eq '') {
chdir '/';
shift @path;
print '/';
$indent = 1;
}
# Now follow the subdirectories and links.
while (@path) {
$elem = shift @path;
$new = readlink($elem);
if (defined $new) { # A symbolic link.
print "$elem -> $new\n";
$new =~ s!^\./!!;
# Prepend symbolic link to rest of path.
unshift(@path,split(m;/;, $new));
# Deal with special cases.
if (@path && $path[0] eq '') {
# Absolute path starts over.
chdir '/';
shift @path;
print '/';
$indent = 1;
@indents = ();
next;
}
# Back up the tree as necessary.
while (@indents && $path[0] eq '..') {
$indent = pop(@indents);
chdir '..'
|| die "\n\nCan't cd to ..: $!\n";
shift @path;
}
print "\t" x ($indent / 8), ' ' x ($indent % 8);
}
else { # An ordinary directory.
print $elem;
push(@indents,$indent);
$indent += length($elem) + 1;
if (@path) {
print '/';
chdir $elem
|| die "\n\nCan't cd to $elem: $!\n";
}
}
}
print "\n\n";
$indent = 0;
chdir $cwd || die "Can't cd back: $!\n" if $cwd ne '';
}
More information about the gnhlug-discuss
mailing list