469,306 Members | 1,748 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,306 developers. It's quick & easy.

Using pwd and chdir to move back to previous directory

I am in unix and in my perl program, I am trying to move back to the directory the program was originally started in. Obviously, at somepoint in my program it has to move to another directory, and now I want to go back.
I tried this lines of perl at the start of the program:
$directorylocation=`pwd`;

and then this command later on when it is time to move back:

chdir $directorylocation;

and it doesn't work. I have checked that $directorylocation does have the correct path and dir. And when I type in the path and directory like this
chdir "g/g14/rdoneil/test";
this line will work. But I can't get the variable to do the job.
(I have also tried
chdir "$directorylocation"; but no-go)

By the way, another option is just moving up two directories - as that is where the program originally started out. But I am thinking there is no easy way to accomplish this.
Sep 26 '07 #1
4 10237
KevinADC
4,059 Expert 2GB
try this:

chdir ($directorylocation) or die "can't chdir to $directorylocation: $!";

and see if you get an error message.
Sep 27 '07 #2
eWish
971 Expert 512MB
Can you show us more code. The code that shows where and how $directorylocation is defined. As well as where it is being used with chdir .

Edit: Man, I was to slow.
Sep 27 '07 #3
mehj123
55
I think the problem is that perl's chdir() function is global.. This means that once you use the chdir() function, it changes the directory for the whole program.. I found that File::chdir module can be used to get a solution to this problem. dont know much.. :)
Sep 27 '07 #4
I am in unix and in my perl program, I am trying to move back to the directory the program was originally started in. Obviously, at somepoint in my program it has to move to another directory, and now I want to go back.
I tried this lines of perl at the start of the program:
$directorylocation=`pwd`;

and then this command later on when it is time to move back:

chdir $directorylocation;

and it doesn't work. I have checked that $directorylocation does have the correct path and dir. And when I type in the path and directory like this
chdir "g/g14/rdoneil/test";
this line will work. But I can't get the variable to do the job.
(I have also tried
chdir "$directorylocation"; but no-go)

By the way, another option is just moving up two directories - as that is where the program originally started out. But I am thinking there is no easy way to accomplish this.

I solved it!

Instead of just doing this:

$directorylocation=`pwd`;

I now add this line:

chomp($directorylocation);

And it now works just fine!
Sep 27 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by John | last post: by
reply views Thread by jeffgeorge | last post: by
8 posts views Thread by moondaddy | last post: by
1 post views Thread by I2 | last post: by
3 posts views Thread by =?Utf-8?B?UHVyZSBIZWFydA==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.