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
AirHead:examples sholden$ cd ..
AirHead:~ sholden$ pwd
AirHead:~ sholden$ 

Was it "set hardpaths?" Thanks in advance.

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


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

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

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

  • +1; fantastic, this is exactly what I tried to find before. – sampson-chen Nov 15 '12 at 20:33
  • Yes, that is indeed what I needed. Thanks a lot for bringing me up to date. I now realie it was the C shell that had that unusual feature. – holdenweb Nov 18 '12 at 9:54

I have never found a way to do it with symlinks, but if your goal is to quickly change between directories in shell, I wrote a blog post recently on several alternatives to accomplish exactly that.

The short answer is to use alias to add something similar to the following to your ~/.bashrc:

alias sf='cd ~/SmileForward/smileforward-backend/sfsite/smileforward'

Or export all the frequently used directories as variables into your ~/.bashrc, and just invoke directly with cd $dir_1 etc:

export dir_1='~/some_long_path_name/more_path_names1`
export dir_2='~/some_long_path_name/more_path_names2`

There's also a tool for switching directories based on frequency / recency called z that I never explored fully.

  • alias or variable works for certain link->directorie(s). but not for all link->directories. – Kent Nov 15 '12 at 20:34
  • @Kent ah; what I was suggesting was to do it w/o symlinks period. – sampson-chen Nov 15 '12 at 20:35
  • in practical I have some variables like your dir_1, dir_2 as well (or put them in cdpath). But not for OP's purpose, just for going to some directory quickly and easily. – Kent Nov 15 '12 at 20:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.