Operating System - HP-UX
1834163 Members
2596 Online
110064 Solutions
New Discussion

Re: Perl: retrieve link info ??

 
SOLVED
Go to solution
David_246
Trusted Contributor

Perl: retrieve link info ??

Hi There,

Hope to be allowed to ask a perl question.
I can use stat() or lstat() to retrieve info about the file, but I have the following problem.

lstat($file) --> tells me all the info of the link
stat($file) --> tells me all the info the file it is linked to

But who do I find out what file it is linked to (in Perl)????
I do not want to find out using a syscal such as ; $f_info=`/usr/bin/ll $file`;
As this is eating resources. Anybody aware of a perl solution for getting the target fname ??

Regs David
@yourservice
3 REPLIES 3
Solution

Re: Perl: retrieve link info ??

Hi David,

the perl function "readlink" will return a string indicating the target of the symbolic link. This may be a relative path from the link location or an absolute path depending upon how the link was set up.
"man perlfunc" gives you basic info on how to use it.

Hope this helps,
Stephen
H.Merijn Brand (procura
Honored Contributor

Re: Perl: retrieve link info ??

perl questions are always welcome.

readlink () gives you the first level of symbolic links. If the target is (again) a symlink, you'd better use the Cwd module:

use Cwd 'abs_path';
my $abs_path = abs_path($file);

# man Cwd

for the full description

Enjoy, have FUN! H.Merijn

Enjoy, Have FUN! H.Merijn
David_246
Trusted Contributor

Re: Perl: retrieve link info ??

Stephen,

Perfect !!! I just couldn't find it.

my $link_target = readlink($link_file);

print "File $link_file is linked to $link_target \n";

Great !!!

Regs David
@yourservice