Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

5 Answers 5

up vote 11 down vote accepted

perheps:

pwd -P

from help:

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

share|improve this answer
    
Freudian slip there? –  Dan Carley Jun 29 '09 at 14:54
    
@Dan C - right. but it's fixed now ;-] [ hopefully ] –  pQd Jun 29 '09 at 14:55
    
Haha. I saw it before the edit and was a bit confused. –  Dan Carley Jun 29 '09 at 14:57
/bin/pwd

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

share|improve this answer
    
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. –  Steve Bennett Jan 24 '12 at 0:10
    
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.) –  Jonathan Leffler Jan 24 '12 at 0:18
    
what's insane about a command behaving consistently regardless of where it's located? –  Steve Bennett Jan 24 '12 at 0:20
    
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 Jan 24 '12 at 0:23
    
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!). –  Jonathan Leffler Jan 24 '12 at 0:30

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

readlink -f PATH
share|improve this answer
    
In the specific case readlink -f . would work just fine. –  mdpc Feb 18 '13 at 18:18

From the shell.

pwd -P

From userland.

/bin/pwd
share|improve this answer

To get a more portable (POSIX-conformant) pwd -P command we could use the regular built-in shell command:

# cf. http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap01.html#tag_17_06

command -p pwd -P
#builtin command -p pwd -P  
#alias pwd=echo; PATH="$(command -p getconf PATH)" 'pwd' -P
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.