开发者

how to get the Absolute Path for symlink file?

开发者 https://www.devze.com 2023-02-08 03:27 出处:网络
#test-> a.pl my $file = \'/home/joe/test\'; if ( -f $file && -l $file ){ print readlink( $fil开发者_如何学JAVAe ) ;

# test-> a.pl

my $file = '/home/joe/test';
if ( -f $file && -l $file )  {
    print readlink( $fil开发者_如何学JAVAe ) ;
}

how to get the Absolute Path for symlink file ?


Cwd provides such functionality by abs_path.

#!/usr/bin/perl -w

use Cwd 'abs_path';

my $file='/home/joe/test';
if( -f $file && -l $file ) {
    print abs_path($file);
}


if you use File::Spec rel2abs along with readlink you'll get the abs path even if it's a symlink to another symlink

use File::Spec;

$path = File::Spec->rel2abs( readlink($file) ) ;
0

精彩评论

暂无评论...
验证码 换一张
取 消