Going into a directory linked by a link

7,279

Solution 1

With POSIX shell, you can use -P option of cd builtin:

cd -P <link>

With bash, from man bash:

The -P option says to use the physical directory structure instead of following symbolic links (see also the -P option to the set builtin command)

Solution 2

Korn-like shells keep track of symbolic links in the path to the current directory (this is known as logical current directory tracking). If you want to expand all symbolic links, pass the option -P to the cd builtin (for physical current directory tracking):

cd -P logic

If you're in a directory which you've accessed via a symbolic link and want to switch the tracked current directory to the path with symbolic links expanded, run

cd -P .

If you want to print the path to the current directory with symbolic links expanded, run pwd -P. In bash, if you want to turn off logical tracking, run set -P; in zsh, run set -w or setopt chase_links.

Solution 3

You can use readlink to determine where your link points, and provide this output as the target of your cd.

cd "$(readlink <link>)"

In the case of additional symlinks pointing to symlinks, readlink will simply provide the target, unless you specify one of it's options to follow symlinks to a canonical file target, for example readlink -f <link>.

readlink - print value of a symbolic link or canonical file name

-f, --canonicalize
canonicalize by following every symlink in every component of the given name recursively; all but the last component must exist

-e, --canonicalize-existing
canonicalize by following every symlink in every component of the given name recursively, all components must exist

-m, --canonicalize-missing
canonicalize by following every symlink in every component of the given name recursively, without requirements on components existence

Share:
7,279

Related videos on Youtube

Tim
Author by

Tim

Elitists are oppressive, anti-intellectual, ultra-conservative, and cancerous to the society, environment, and humanity. Please help make Stack Exchange a better place. Expose elite supremacy, elitist brutality, and moderation injustice to https://stackoverflow.com/contact (complicit community managers), in comments, to meta, outside Stack Exchange, and by legal actions. Push back and don't let them normalize their behaviors. Changes always happen from the bottom up. Thank you very much! Just a curious self learner. Almost always upvote replies. Thanks for enlightenment! Meanwhile, Corruption and abuses have been rampantly coming from elitists. Supportive comments have been removed and attacks are kept to control the direction of discourse. Outright vicious comments have been removed only to conceal atrocities. Systematic discrimination has been made into policies. Countless users have been harassed, persecuted, and suffocated. Q&amp;A sites are for everyone to learn and grow, not for elitists to indulge abusive oppression, and cover up for each other. https://softwareengineering.stackexchange.com/posts/419086/revisions https://math.meta.stackexchange.com/q/32539/ (https://i.stack.imgur.com/4knYh.png) and https://math.meta.stackexchange.com/q/32548/ (https://i.stack.imgur.com/9gaZ2.png) https://meta.stackexchange.com/posts/353417/timeline (The moderators defended continuous harassment comments showing no reading and understanding of my post) https://cs.stackexchange.com/posts/125651/timeline (a PLT academic had trouble with the books I am reading and disparaged my self learning posts, and a moderator with long abusive history added more insults.) https://stackoverflow.com/posts/61679659/revisions (homework libels) Much more that have happened.

Updated on September 18, 2022

Comments

  • Tim
    Tim almost 2 years

    When I cd a link, my current path is prefixed with the link's path, rather than the path of the directory the link links to. E.g.

    ~/dirlinks/maths$ ls -l logic
    lrwxrwxrwx 1 tim tim 71 Jul 27 10:24 logic -> /windows-d/academic discipline/study objects/areas/formal systems/logic
    
    ~/dirlinks/maths$ cd logic
    ~/dirlinks/maths/logic$ pwd
    /home/tim/dirlinks/maths/logic
    
    ~/dirlinks/maths/logic$ cd ..
    ~/dirlinks/maths$
    

    I would like to have my current path changed to the path of the linked dir, so that I can work with the parent dirs of the linked dir as well. Besides ls the link to find out the linked dir, and then cd into it, what are some simpler ways to accomplish that? For example, after cd into a link, how do you change your current path to the path of the linked dir?

  • ctrl-alt-delor
    ctrl-alt-delor almost 10 years
    You can do this cd -P . after the original cd.