0

I would like to get the symbolic link full path.

Consider the following:

/path/to/folder/link.file links to /path/to/folder/original.txt.

I need to acquire the links' full path and not the original file, such as:

$fullpath = abs_path("link.file");

will return /path/to/folder/link.file instead of /path/to/folder/original.txt.

I tried reading but couldn't get a working solution..

To clarify, the absolute path of the link file itself

Nisan Bahar
  • 73
  • 1
  • 1
  • 8
  • 1
    [readlink](https://perldoc.perl.org/functions/readlink.html) – zdim Nov 11 '18 at 08:11
  • This returns the real path for the original file. – Nisan Bahar Nov 11 '18 at 09:50
  • 1
    @jww, the asker wants to get absolute path to the **link file**, not to the *target file* like in the referenced question. – Tsyvarev Nov 11 '18 at 16:18
  • The `/path/to/folder/link.file` is the "full" (absolute) path since it starts with a `/` so it appears that you don't want that. When you say "_.. will return `/path/to/folder/link.file` instead of `/path/to/folder/original.txt`_" it appears that you have the absolute path of the link but want the absolute path of the target (contrary to what the previous sentence states). – zdim Nov 11 '18 at 17:57
  • @jww was right. I want the full absolute path of the link file and not the original file it points to. – Nisan Bahar Nov 12 '18 at 07:24
  • 1
    @jww not a duplicate, since the link relates to absolute path of the original file – Nisan Bahar Nov 12 '18 at 07:25
  • Yes, @Tsyvarev is right. I want the absolute path of the link file itself like states in the post – Nisan Bahar Nov 12 '18 at 07:28

1 Answers1

2

Use a method that doesn't check the file system such as File::Spec::Functions's rel2abs.

[~]$ perl -MCwd=abs_path -e'CORE::say abs_path($ARGV[0])' tmp
/tmp/ikegami

[~]$ perl -MFile::Spec::Functions=rel2abs -e'CORE::say rel2abs($ARGV[0])' tmp
/home/ikegami/tmp

Of course, if you can guarantee it's a relative path, you could simply use the following:

[~]$ perl -MCwd=getcwd -e'CORE::say getcwd()."/".$ARGV[0]' tmp
/home/ikegami/tmp
jww
  • 97,681
  • 90
  • 411
  • 885
ikegami
  • 367,544
  • 15
  • 269
  • 518