#!/usr/bin/perl use warnings; use strict; use feature qw{ say }; use Path::Tiny qw{ path }; my $path = path(shift); my $paths = $path->visit( sub { $_[1]->{$_} = (-f) ? 'file' : (-l) ? [readlink] : (-d) ? 'dir' : 'unknown' }, { recurse => 1 } ); my @links; for my $found (sort { length $a <=> length $b } keys %$paths) { if ('file' eq $paths->{$found}) { say qq(cp '$found' "\$target/$found"); } elsif ('dir' eq $paths->{$found}) { say qq(mkdir "\$target/$found"); } elsif (ref [] eq ref $paths->{$found}) { my $to = path($paths->{$found}[0]); $to = $to->relative(path($found)->absolute->parent) if $to->is_absolute; my $source = path('$target', $found); push @links, qq(ln -s "$to" "$source"); } } say for @links;