Resolving symbolic links (pwd)

Say I do the following:

cd /some/path
ln -s /target/path symbolic_name

If then do:

cd /some/path
cd symbolic_name
pwd

I get:

/some/path/symblic_name

and not:

/target/path

Is there a way to have the shell “fully resolve” a symbolic link (i.e. updating CWD, etc.), as if I had directly done:

cd /target/path

?

I need to run some programs that seem to be “aware” or “sensitive” about how I get to my target path, and I would like them to think that I arrived to the target path as if had done cd /target/path directly.

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

Your shell has a builtin pwd, which tries to be “smart”. After you did a cd to a symlink the internal pwd fakes the output as if you moved to a real directory.

Pass the -P option to pwd, i.e. run pwd -P. The -P option (for “physical”) tells pwd not to do any symbolic link tracking and display the “real” path to the directory.

Alternatively, there should also be a real binary pwd, which does not do (and is even not able to do) this kind of magic. Just use that binary explicity:

$ type -a pwd
pwd is a shell builtin
pwd is /bin/pwd
$ mkdir a
$ ln -s a b
$ cd b
$ pwd
/home/michas/b
$ /bin/pwd
/home/michas/a

Method 2

Try cd -P <symlink_dirname>.

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="72061b1f321e01">[email protected]</a>:~$ mkdir a
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="86f2efebc6eaf5">[email protected]</a>:~$ ln -s a b

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1165787c517d62">[email protected]</a>:~$ cd b
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5d2934301d312e">[email protected]</a>:~/b$ pwd
/home/tim/b

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a5d1ccc8e5c9d6">[email protected]</a>:~/b$ cd ..
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="4034292d002c33">[email protected]</a>:~$ cd -P b
<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="9ce8f5f1dcf0ef">[email protected]</a>:~/a$ pwd
/home/tim/a

You can also use set -o physical to make this behavior persist through the lifetime of the running shell.

Check out https://stackoverflow.com/questions/10456784/behavior-of-cd-bash-on-symbolic-links for some more good info.


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
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x