bug-fileutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Strange goings on with the humble link...


From: Bob Proulx
Subject: Re: Strange goings on with the humble link...
Date: Thu, 13 Feb 2003 11:24:24 -0700
User-agent: Mutt/1.3.28i

Simon Egerton wrote:
> 
> I would like to attain how a symbolic link to a directory should be
> dereferenced.  I give an example, 

That depends.  You need to choose whether you want an absolute or
relative path.  Most of the time you want a relative path.  But that
does not seem tobe your question.

> 1) From /
> 2) mkdir etc2
> 3) cd etc2
> 4) ln -s /etc/sysconfig 
> 5) echo > myfile.txt
> 6) cd sysconfig
> 7) ln -s ../myfile.txt linktomyfile.txt
> 
> Now, if I type pwd "/etc2/sysconfig" is returned, if using the "bash" shell
> I type ls ../ [tab] the content of /etc2 is listed, if I type ls ../
> [return], the content of /etc is listed, if I type cd .., I return to /etc2,
> the link created in step 7 dereferences from /etc/sysconfig and not
> /etc2/sysconfig.

Yes.

> To me this behaviour is inconsistent, is this a bug?

It is a designed in feature of the shell.  Whether it is a bug or not
depends upon whether you like the feature or not.  It has been a topic
of debate on the net.

This feature started with the Korn shell to which bash is compatible.
The shell is trying to help you out.  For the purpose of showing the
present working directory with pwd the shell is remembering how you
got there.  When you cd '..' it removes one layer of directory from
the end.  Effectively changing 'cd ..' into 'cd $(dirname $PWD)'.

However it is only that shell and nothing else that does that.  Any
real reference through .. will give you the Real Path results and not
the Fake Path results of the shell.

> 1) From /
> 2) mkdir etc2
> 3) cd etc2
> 4) ln -s /etc/sysconfig 
> 5) echo > myfile.txt
> 6) cd sysconfig

Right here try this to see the difference.

  pwd
  pwd -P
  /bin/pwd

The first one will say /etc2/sysconfig.  But the second and third will
say /etc/sysconfig (if I got that right).  By changing into the
directory through the symlink you are actually sitting in the /etc
directory and not etc2.  The shell knows you got there by the route of
/etc2 and keeps track of that in the PWD variable.  But no other
command has any knowledge of that, nor cares.  All other commands will
use the Real Path as returned by pwd -P and /bin/pwd.

You can turn that fake pwd routing off in the shell.  Read the shell
documentation on 'cd -P', 'pwd -P' and the 'set -P' shell option.

  set -P

> If this is intended, then how may I create a link that would
> dereference from the "pwd" path rather than the path pointed to by
> the link? i.e. make the link created in step 7 work?

In short, you can't.  Your .. is not the same .. as before.  However,
symbolic links just do name translations on filenames.  You can
probably use absolute paths to get to the file.

Note that full absolute paths do not translate across NFS mounted
filesystems.  Only relative paths do.  I will leave that as an
exercise to the reader.

Bob





reply via email to

[Prev in Thread] Current Thread [Next in Thread]