#! /usr/bin/perl use strict; use warnings; use Getopt::Std; use File::Copy; use File::stat; use File::Spec; use IO::Dir; use Data::Dumper; my %opt; getopts('dh', \%opt); @ARGV = grep { $_ ne '.' and $_ ne '..' } map { s.[/\\]$..g; $_ } @ARGV; foreach my $l (@ARGV) { unless (((lstat $l)->nlink() > 1 && -f _) || -l _) { print "$l isn't a link - skipping\n"; next; } if (-d $l) { unless (exists $opt{d}) { print "$l is a directory and -d wasn't specified - skipping\n"; next; } my $S = File::Spec->catdir(File::Spec->rel2abs(readlink($l))); unlink $l; mkdir $l; if (fork()) { chdir($l); my $Sdir = IO::Dir->new($S); while (defined (my $f = $Sdir->read)) { my $Sfile = File::Spec->catfile($S, $f); if (exists $opt{h} and -f $Sfile) { # if the hardlink fails, fall back to symlink link $Sfile, $f or symlink $Sfile, $f; } else { symlink $Sfile, $f; } } } } else { my $stat = stat($l); rename $l, "$l.old"; copy("$l.old", $l); unlink "$l.old"; utime $stat->atime, $stat->mtime, $l; chmod $stat->mode | 0200, $l; } } 1 while wait != -1;