How to find the actual working directory?

7,989

Solution 1

perheps:

pwd -P 

from help:

"-P : The pathname printed will not contain symbolic links. "

Solution 2

To solve this problem for the general case (i.e. not just current directory), use:

readlink -f PATH

Solution 3

/bin/pwd

It doesn't know where you've been or how you got there, so it works it out from first principles.

Solution 4

From the shell.

pwd -P

From userland.

/bin/pwd
Share:
7,989

Related videos on Youtube

Paul
Author by

Paul

Updated on September 17, 2022

Comments

  • Paul
    Paul over 1 year

    When I try to find the current working directory at a linux command prompt (using "pwd"), it will show the directory with symbolic links included. For example, if I make a symbolic link and use it to visit that directory:

    ln -s /mnt/backup /home/biotech/backup
    cd /home/biotech/backup
    pwd
    

    This will show "/home/biotech/backup" instead of "/mnt/backup". This happens on both Ubuntu and Cygwin.

    What command can I use to see the latter, the "real" directory?

  • Dan Carley
    Dan Carley almost 15 years
    Freudian slip there?
  • pQd
    pQd almost 15 years
    @Dan C - right. but it's fixed now ;-] [ hopefully ]
  • Dan Carley
    Dan Carley almost 15 years
    Haha. I saw it before the edit and was a bit confused.
  • Steve Bennett
    Steve Bennett over 12 years
    Which environment does this work in? For me on OSX, "which pwd" = /bin/pwd, so calling it directly makes no difference. You need the -P flag.
  • Jonathan Leffler
    Jonathan Leffler over 12 years
    Anything sane - which apparently does not include Mac OS X. I've just tried it, and to my complete and utter horror, your observation is correct. I hate being nannied like that. It is completely obnoxious. Somehow, /bin/pwd is being told how you did your cd (on MacOS X). That is so infuriating I can hardly type coherently. Pusillanimous! Butchery of civilized system. Grrrrrrr!!!!!!!!!!!!!!!!!! (The exact same test on a Linux worked as stated. Linux is sane. And I mainly use MacOS X; I don't get this infuriated with it easily.)
  • Steve Bennett
    Steve Bennett over 12 years
    what's insane about a command behaving consistently regardless of where it's located?
  • Jonathan Leffler
    Jonathan Leffler over 12 years
    It means I have to unlearn close to 30 years of sane behaviour and start mapping it to handle a system that deviates unnecessarily from the path of the old ones (like me). I've no particular problem with the shell knowing how I got to a specific directory, though that sometimes annoys me to. I have major problems with a command like /bin/pwd telling me how it got to the directory; it should work out where it is in the directory hierarchy. Basically, I've now got to know how to find out the real path on MacOS X differently from everywhere else. Which is an utter pain for portability.
  • Jonathan Leffler
    Jonathan Leffler over 12 years
    And, just for the record, it is MacOS X (maybe BSD) vs 'the rest'; HP-UX, AIX, Solaris and Linux all behave as documented here. And if I had to guess, so will plain BSD; I'd lay odds it is a Mac-only 'feature' (full pejorative intent completely intended!).
  • Vijay Anand Mareddy
    Vijay Anand Mareddy about 11 years
    don't have an OSX system here, but perhaps it helps if you call it like PWD= /bin/pwd (with the space, i. e. unset PWD environment variable )?
  • mdpc
    mdpc about 11 years
    In the specific case readlink -f . would work just fine.
  • Jonathan Leffler
    Jonathan Leffler about 11 years
    Interestingly, that (setting PWD empty, or unsetting it altogether) does work on Mac OS X, as does setting PWD to an invalid value. It is still frustrating to have to use a different command on Mac OS X to find the real path for the current directory compared with every other Unix-like system.