#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0

    Get full path of a symlink to a file - again


    Hi

    I have an file <foo.jpg> stored in a directory:
    /var/images/cam1/
    I have a symliink lastsnap.jpg in the same directory that points to foo.jpg.

    I want to get the full pathname of the file : /var/images/cam1/foo.jpg.

    So I tried perl readlink(). This just returns the filename foo.jpg without the path.

    So I tried perl realpath( readlink "lastsnap.jpg") and got:
    </home/user/foo.jpg > which is just wrong.


    I know that the file is located in </var/images/cam1/> so I could just concatenate this with the file name, but that wouldn't make good code.

    What I need is an equivalent to the linux <readlink -f >command.
    This does give the answer I am looking for.

    How do I do this in perl????

    I have searched the internet and found others asking the same questions but I have not found an answer that works.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Apr 2009
    Posts
    1,938
    Rep Power
    1225
    Try this:
    Code:
    use File::Spec;
    
    # assuming $file is defined prior to this point
    my $path = File::Spec->rel2abs( readlink($file) ) ;
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2007
    Posts
    765
    Rep Power
    929
    I don't have my *nix system up to test at the moment, but I suspect that "realpath($filename)" is what you want (the call to readlink being unnecessary since realpath probes the filesystem itself and harmful since it discards the path data realpath needs to determine where the symlink points).
    sub{*{$::{$_}}{CODE}==$_[0]&& print for(%:: )}->(\&Meh);
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2013
    Posts
    16
    Rep Power
    0
    Hi

    Thanks.

    realpath(<foo.file>) is the solution. It does give the full path name.

    Dazz

IMN logo majestic logo threadwatch logo seochat tools logo