3

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?

pQd
  • 29,981
  • 6
  • 66
  • 109
Paul
  • 133
  • 1
  • 6

6 Answers6

16

perheps:

pwd -P 

from help:

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

pQd
  • 29,981
  • 6
  • 66
  • 109
5
/bin/pwd

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

Jonathan Leffler
  • 1,035
  • 11
  • 20
  • 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 00: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 00:18
  • what's insane about a command behaving consistently regardless of where it's located? – Steve Bennett Jan 24 '12 at 00: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 00: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 00:30
  • 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 )? – mihi Feb 18 '13 at 17:41
  • 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. – Jonathan Leffler Feb 18 '13 at 18:54
5

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

readlink -f PATH
MikeyB
  • 39,291
  • 10
  • 105
  • 189
3

From the shell.

pwd -P

From userland.

/bin/pwd
Dan Carley
  • 25,617
  • 5
  • 53
  • 70
0

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
carlo
  • 1
0

On windows7, I use cmd with numerous NTFS hardlinks to folders, cygwin, bash and perl are in my path, but pwd -P doesn't work as suggested above. This script walks up the tree and derefs links to realdir:

CHN=$(cygpath -wam .)
echo $CHN
while true ;do
  OLD=$DIR
  DIR=$(cygpath -wam .) 
  if [[ ( -n "$OLD" ) && !( $OLD =~ $DIR* )]] ;then
    SYM=$DIR/${OLD##*/}
    CHN=$(echo $CHN | perl -lpe "s,$OLD,$SYM,")
    echo $CHN
  fi
  case $DIR in [A-Z]:/ | [A-Z]:/cyg* ) break ;; esac # TOPLEVEL
  cd $DIR/..
done 
mosh
  • 111
  • 3