Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a symlink

~/link -> ~/a/really/long/path

When I do

cd ~/link
cd ..    

it takes me to

~

but I want to go to

~/a/really/long

Is there a way to do this?

I am using bash.

share|improve this question
    
You mean cd ~/a/really/long/path then cd .. puts you in ~ at the moment? I can't reproduce that. –  Mikel Apr 11 '11 at 5:13
    
@Mikel: No, first create a symlink to ~/a/really/long/path then cd to that then to ... You should end up back in ~ (assuming that's where you started). –  Xodarap Apr 11 '11 at 5:50
    
Is my edit accurate? I couldn't understand the question before. –  Mikel Apr 11 '11 at 6:28
    
@Mikel: yes, this is correct. Thanks! –  Xodarap Apr 11 '11 at 15:34
add comment

3 Answers

up vote 18 down vote accepted

Bash (as well as ksh, zsh, and even ash) track directory changes so that cd /foo/bar && cd .. always takes you to /foo even if bar is a symlink. Pass the -P option to cd to ignore the tracked change and follow the “physical” directory structure:

cd -P ..

See help cd or man builtins for documentation about the bash builtin cd. If you really dislike the directory tracking feature, you can turn it off with set -P in bash (set -o no_chase_link in zsh).

share|improve this answer
    
The way I read the question, the OP has -P or -o physical already set? –  Mikel Apr 11 '11 at 5:14
    
+1, but suggest link to man page. –  Faheem Mitha Apr 11 '11 at 8:00
    
@Mikel: No, the question says that he is following the symbolic link, not the physical directory structure. –  Faheem Mitha Apr 11 '11 at 8:02
    
Sorry, I didn't actually try to edit your post. Suggested edit submitted now. –  Faheem Mitha Apr 11 '11 at 10:00
add comment

One method you could use is to use an alias instead of a symlink to take you to ~/a/really/long/path , that's the method I use - that way I can just type a simple 1/2/et cetera letter command instead of cd symlink

share|improve this answer
add comment

You can also use readlink to find the physical path to this directory, then go one directory higher:

cd $(readlink -f .)/..
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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