bug-fileutils
[Top][All Lists]
Advanced

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

Re: Difference between cd and ls regarding symbolic links


From: Bob Proulx
Subject: Re: Difference between cd and ls regarding symbolic links
Date: Fri, 25 Jan 2002 22:20:53 -0700

> I don't know if this is a bug but here goes:

No it is not a bug.  It is working the way it is supposed to work.
But you are not the first person to be confused by it.  Note that this
has nothing to do with fileutils but rather symlinks and your comamnd
shell.

> $ ln -s /tmp/test/dir1/dir2 /tmp/test/dir2
> $ cd dir2
> $ pwd 
> /tmp/test/dir2

That is how you got there.  But not where you really are.  Try running
the following:

  pwd -P
  /bin/pwd

Read the documentation on pwd -P (as 'info bash').  Here is a snippet.
Also check out the documentation for cd [-LP] too.

 pwd [-LP]
     Print the absolute pathname of the current working directory.  If
     the `-P' option is supplied, the pathname printed will not contain
     symbolic links.  If the `-L' option is supplied, the pathname
     printed may contain symbolic links.  The return status is zero
     unless an error is encountered while determining the name of the
     current directory or an invalid option is supplied.

Many people use the shell option 'set -P' command to force the old
behavior.  Old and new being relative, of course.  This is purely
personal preference.

> $ ls -l ..
> total 4
> drwxr-xr-x    2 bclark   bclark       4096 Jan 25 12:25 dir2

The '..' file is the parent of the directory you are -really- in,
which is where the symlink took you.  The ls command lists it as
requested.  

> $ cd ..
> $ pwd
> /tmp/test

The shell remembers how you got to someplace by keeping it in a PWD
environment variable.  When you 'cd ..' it actually does something
more like this.  This unwraps the last entry of the directory by the
route you used to get there.

  cd `dirname $PWD`

> How come ls .. does not list the contents of the directory I go to when I do
> a cd .. ?

The cd command is a built-in to the shell and tracks $PWD.  The ls
command is an external command and cannot use $PWD.  It must use the
real /bin/pwd [actually getcwd(3)] path since at that time the PWD
variable cannot be trusted.  It cannot be trusted because only the
shell updates it.  For example if a perl script were to run `ls` after
running chdir('someplace') then the PWD variable will not be correct
and therefore can only be ignored.

Bob



reply via email to

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