19

I created a soft link (ln -s 1 2) to a directory which is inside the test directory and opened the soft link (cd 2) and displayed the current path using pwd. The displayed path was ~/test/2 and not ~/test/1.

It's different in an OS like Windows, the shortcut brings us to the real directory. I'm little bit confused how this soft link works in Linux. Is it not a shortcut like in Windows? Why is the path not ~/test/1?

 $ mkdir test
 $ cd test
 $ mkdir 1
 $ ln -s 1 2
 $ cd 2
 $ pwd
 /home/dazz/test/2
  • 2
    Thanks for removing the screenshot. Next time, please edit your question instead of deleting and reposting. Deleting too many of your own questions can get you banned from asking. That ban is automatic so we won't be able to do much about it. In any case, you don't need to repost since editing bumps the question. – terdon Apr 16 '15 at 17:29
  • Why do you ask twice? askubuntu.com/q/610133/367165 – A.B. Apr 16 '15 at 17:30
  • A.B same forum? – DScript Apr 16 '15 at 17:53
18

That is a feature of the shell that remembers how you got to where you are.
If you have realpath installed you can do:

$ realpath 
/home/dazz/test/1

And lacking that if you have python:

$ python -c "import os; print(os.path.realpath('.'))"
/home/dazz/test/1

or readlink (from coreutils):

$ readlink -f .
/home/dazz/test/1

or /bin/pwd (not the shell built-in pwd):

$ /bin/pwd
/home/dazz/test/1
  • ls -l /proc/$$/cwd takes advantage of the kernel's /proc filesystem interface. – Bruce Ediger Apr 16 '15 at 19:01
  • 8
    Note also pwd -P to print the physical, instead of logical, path to the current directory. – godlygeek Apr 16 '15 at 21:17
8

Contrast pwd and /bin/pwd.

pwd, which is a built-in command in many shells, tells you where your shell thinks you are (and hence treats symlinks "soft links" as if they were real directories.

/bin/pwd is an external program that tells you where you really are, if necessary by traversing the filesystem tree up to /. It takes no account of symlinks because those don't exist when traversing upwards through ..

6

Other answers have covered /bin/pwd vs the shell's builtin pwd.

If you want to follow symlinks in the Windows style you mentioned, use cd -P: it will change the PWD variable accordingly.

If you want to use -P by default, you can add this line to your .bashrc or .zshrc:

set -P

Other shells may vary.

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.