Changing parent directory (../) with symlinks

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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).

Method 2

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

cd $(readlink -f .)/..

Method 3

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, since then I can just type a simple 1/2/et cetera letter command instead of cd symlink


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments