Silly question...move up a directory...

Discussion in 'Perl Misc' started by John, Sep 25, 2003.

  1. John

    John Guest

    Sorry, but how does one move up a directory ?

    I'm in a directory from which I called a script. Part of the script
    functionality is checking the permissions of the directory I am in. I may
    need to change the directory permissions depending on what they are. Hence,
    I need to be able to move up [and possibly down] one level of the directory

    chdir? Something like cd .. would be nice

    Thank you.
    John, Sep 25, 2003
    1. Advertisements

  2. John

    D Borland Guest

    Sorry, but how does one move up a directory ?
    You got it chdir

    chdir '/home/john/scripts/' or die "Can't cd to /home/john/scripts/: $!\n";

    D Borland, Sep 25, 2003
    1. Advertisements

  3. John

    John Guest

    Thanks. But if the script does not automatially know which directory it's
    in, would I need to pass the full path to it first, then sed it to drop the
    last directory and then use chdir on the remaining part from sed? Seems
    awfully complicated for such a simple task... :(

    Using your example, if the script does not know it's in home/john/scripts/
    but I need to move up to home/john, then would I need to use the full path +
    sed combo outlined in my first paragraph?
    John, Sep 25, 2003
  4. It was a dark and stormy night, and John managed to scribble:
    chdir '..';

    Gregory Toomey, Sep 25, 2003
  5. John

    Dan Rawson Guest

    John -

    use Cwd;

    my $currdir = getcwd();
    chdir '..';
    chdir $currdir;

    perldoc Cwd

    Dan Rawson, Sep 25, 2003
  6. John

    D Borland Guest


    Truthfull depends on exactly what your requirements are, i'm fairly new to
    perl myself so my suggestion would probably be to move up a directory you
    could just do

    chdir '..' or die "$!\n";

    to move through the directory you could always use 'opendir' and 'readdir'
    to find new directories to explore - see perlfunc man for these functions.

    But like i said i'm fairly new to perl and i haven't really thought about it
    too much as i'm at work at the moment, but i'm sure there will be something
    on CPAN or already in perl in to this better.

    D Borland, Sep 25, 2003
  7. John

    John Guest

    Thanks very much guys! I knew it had to be simple...
    John, Sep 25, 2003
  8. John

    John Guest

    Thanks Greg.
    John, Sep 25, 2003
  9. BING! You have earned yourself an entry in the Perl SAQ :)
    Peter Sundstrom, Sep 26, 2003
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.