2013-04-18 55 views
0

我需要更改给定目录中的所有符号链接以使用最短的相对路径。更改符号链接

例子: 变化

kat/../kat/link 

usr/sth/sth/kat/link 

kat/link 

我怎样才能做到这一点使用Perl?

+0

不是珍珠..这是Perl – Futuregeek

+0

是的,thx为编辑:) – Kmaczek

+1

你试过了什么? – devnull

回答

2

您可以通过使用abs_path然后删除当前目录,使其相对于得到一个简化路径:

use warnings; 
use strict; 
use Cwd qw/getcwd abs_path/; 

my $silly_path = 'foo/../foo/../foo/../foo'; 

my $simplified = abs_path($silly_path); 
my $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"; 

这假设链接是在Perl的当前工作目录。如果需要,您可以用另一个目录替换它。它将导致当前目录中链接的相对路径,或者指向当前目录之外的某个简化绝对路径。

您可以使用glob获取目录中的所有文件,然后使用-l $filefile test operator来测试$file是否是符号链接。