perl - Change symbolic links -
i need change symbolic links in given directory use shortest relative path.
example: change
kat/../kat/link
or
usr/sth/sth/kat/link
into
kat/link
how can using perl?
you can simplified path using abs_path
, removing current directory make relative:
use warnings; use strict; use cwd qw/getcwd abs_path/; $silly_path = 'foo/../foo/../foo/../foo'; $simplified = abs_path($silly_path); $cwd = getcwd(); print "canonical path: $simplified\n"; print "current directory: $cwd\n"; $simplified =~ s|^\q$cwd/||; #make relative if within current directory. print "simplified path: $simplified\n";
this assumes links in perl's current working directory. replace directory if want. result in relative path link within current directory, or simplified absolute path points outside current directory.
you can files in directory using glob, use -l $file
file test operator test if $file
symbolic link.
Comments
Post a Comment