Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

symlink woes

by FredKJ (Novice)
on Aug 22, 2012 at 15:22 UTC ( #989075=perlquestion: print w/replies, xml ) Need Help??
FredKJ has asked for the wisdom of the Perl Monks concerning the following question:

I am working on code that collects, gzips, and moves files around. Pretty standard stuff. Problem is the directory structure I'm working with is a symlinked one. So for instance I can do a ch_dir to var/cores but if I do a $path = `pwd` I will get vtss/coresfs. I can't just use file module commands for consistency because I also need to use sudo in many cases. So I can make the code work but if you are using two references for you location it makes the code confusing. Is there a way I can tell my code to use the hard paths or linked paths exclusively?

Replies are listed 'Best First'.
Re: symlink woes
by johngg (Abbot) on Aug 22, 2012 at 17:03 UTC

    You could use lstat to determine whether your directory is actually a symlink to a directory and readlink to find out where it is pointing. The standard Cwd module might also allow you to keep better track of which directory you are in. I'm not sure how useful these pointers will be when having to interact with sudo but, hopefully, they will help you move forward.



      Maybe expanding on something like:
      use File::Find; # might be helpful use Cwd; # also pretty useful with this sort of thing my $files = <some array here...>; my $where = readlink("$files"); print "$files => $where\n";
      #!/usr/bin/perl -Tw
      use perl::always;
      my $perl_version = "5.12.4";
      print $perl_version;
Re: symlink woes
by cheekuperl (Monk) on Aug 22, 2012 at 15:41 UTC
    I don't have environment to try this out, but afaik, a symlink is a "file" that contains path to another file (simply put).
    So when you chdir, it does change to the directory that you asked for, and `pwd` too, does its job honestly. So, it seems nothing is wrong with the functions. I think expecting `pwd` to return the path of symlink is not correct, because symlink is a plain file and not a directory file :)
      Of course then I am doing a system sudo call which is spawning a root shell with another context.
Re: symlink woes
by graff (Chancellor) on Apr 24, 2013 at 06:01 UTC
    Is there a way I can tell my code to use the hard paths ... exclusively?

    If I understand correctly, you want to do a "typical" chdir (as a typical user) to put yourself into a directory via a symlinked path, and then be able to use a consistent path string from this point, even while invoking a sudo process.

    Try something like this maybe:

    #!/usr/bin/perl use strict; use warnings; use Cwd; my $startup_path = getcwd; my $symlink_path = "some/symlink/path"; chdir $symlink_path; my $working_path = getcwd; printf( "Started in %s, chdir'd to %s, now in %s\n", $startup_path, $symlink_path, $working_path );
    I think it should be the case that the value of "$working_path" should always be the absolute, physical path, without any symlink component in it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://989075]
Approved by Tanktalus
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2017-05-30 11:24 GMT
Find Nodes?
    Voting Booth?