Change into symlink directory - how?

Hello all,

how to I change into a symlink directory?

In the bash I wrote:

mkdir abc
ln -s abc abclink

Now in ruby:

Dir.chdir(“abclink”)
Dir.pwd
=> “…/abc”

I want to change in “abclink”, not “abc” like it’s done in the bash:

cd abclink
pwd
=> "…/abclink

How can I do that in ruby?

Thanks,
DaAlex

On Aug 18, 2011, at 02:58 , Alexander S. wrote:

Dir.pwd
=> “…/abc”

I want to change in “abclink”, not “abc” like it’s done in the bash:

cd abclink
pwd
=> "…/abclink

How can I do that in ruby?

that’s just a feature of bash (which I always turn off with set -P).
It doesn’t mean you’re actually in abclink, just that it looks that way.

I know of no way to make chdir/pwd work that way in ruby.

Thanks for the answer.

I hoped that softlinks are “transparent” to the user. I have mapped
several folders into ONE root folder.

In the bash I can switch between these folders very easily (cd …/a, cd
…/b)
–> In ruby I cannot switch between these folders because the resulting
path is the target of the symlink. This is not very nice.

Imho “chdir(mydir)” should change into the softlink dir and
“chdir(File.readlink(mydir))” should change into the target dir.

Maybe anyone else knows a trick or has an idea? Or is that a feature
request?

Try the following:

Open a bash shell:
#> mkdir foo
#> ln -s foo bar
#> cd bar

Now the bash shell hopefully displays something like: “~/bar”

Then check the “real” pwd for the current bash shell.
#> stat /proc/$$/cwd

File: /proc/<pid>/cwd' ->/home//foo’
… snip …

This excercise shows that there is no such notion as a symlinked working
directory, it’s just bash being clever. I.e. the system calls always
resolve
the soft links (or ELOOP), but bash manages the state of the soft links.

If you look into the documentation of ls and cd you see the options
-L/-P
(as mentioned earlier).

If you want another excercise (go back to the original directory), try:
#> mkdir temp
#> ln -sf $PWD/foo temp/bar
#> cd temp/bar
#> ls …

You might be surprised that the directory being listed is actually the
initial $PWD, not $PWD/temp. This is because ls usually is not a bash
built-in, but a real command.

The result: if you want symlink magic, you have to do it like bash; the
hard
way : ).
Hope this clears things up!

– John-John T.

Thanks for the explanation, I understand the problem now.

However, is there ANY (hard) way to get the current symlinked directory
and not the symlink target in a ruby script (Dir.pwd returns “only” the
target)?

On Mon, Sep 19, 2011 at 4:55 PM, Alexander S. [email protected]
wrote:

Thanks for the explanation, I understand the problem now.

I am not sure: why do you ask then? :slight_smile:

However, is there ANY (hard) way to get the current symlinked directory
and not the symlink target in a ruby script (Dir.pwd returns “only” the
target)?

As JJ said: you need to do it like bash and do it manually (i.e. keep
track of your idea of the current working directory path).

Kind regards

robert

The only solution I’ve found is to ask Bash itself, using ENV[‘PWD’],
however that doesn’t work if you’re changing the working directory
within Ruby itself. In that case I imagine you’d have to override
Dir.chdir and keep track of things manually.

On Mon, Aug 22, 2011 at 8:52 PM, Alexander S. [email protected]
wrote:

–> In ruby I cannot switch between these folders because the resulting
path is the target of the symlink. This is not very nice.

care to post your ruby script, pls.
kind regards -botp

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs