How to get cd to switch to the full path when following symbolic links

shellsymbolic-link

How can I get the shell to understand the full path after following a symbolic link. In the example below I'd like the first "pwd" command to report "/Users/sholden/Projects/PS1/examples", and the "cd .." command to take me to /Users/sholden/Projects/PS1/ rather than back to my home directory. I have a dim memory of a setting that did this in the Bourne shell (or was it Korn?*) but I'm having trouble dredging up the details from 25 years ago …

AirHead:~ sholden$ ls -ld examples
lrwxr-xr-x  1 sholden  staff  53 Nov 15 09:33 examples -> /Users/sholden/Projects/PS1/examples
AirHead:~ sholden$ cd examples
AirHead:examples sholden$ pwd
/Users/sholden/examples
AirHead:examples sholden$ cd ..
AirHead:~ sholden$ pwd
/Users/sholden
AirHead:~ sholden$ 

Was it "set hardpaths?" Thanks in advance.

* It was neither – I was a csh user back then.

Best Answer

you should use cd with -P option to do that.

from man pange:

 -P     Handle the operand dot-dot physically; symbolic link components shall be resolved before dot-dot components
              are processed (see step 7. in the DESCRIPTION).

small test to show how does it work:

kent@ArchT60:/tmp$ ls -ld /opt/google/picasa/
drwxr-xr-x 3 root root 4096 Aug  5  2010 /opt/google/picasa/

kent@ArchT60:/tmp$ ln -s /opt/google/picasa/ cdLink
kent@ArchT60:/tmp$ ls -l cdLink
lrwxrwxrwx 1 kent kent 19 Nov 15 21:23 cdLink -> /opt/google/picasa/

kent@ArchT60:/tmp$ cd -P cdLink
kent@ArchT60:/opt/google/picasa$ pwd
/opt/google/picasa

kent@ArchT60:/opt/google/picasa$ cd /tmp/cdLink
kent@ArchT60:/tmp/cdLink$ 
kent@ArchT60:/tmp/cdLink$ cd -P ..
kent@ArchT60:/opt/google$ 

i think the example above showed what you are looking for.

Related Question