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?

6 Answers 6

16

perheps:

pwd -P 

from help:

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

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

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

7
  • 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. Jan 24, 2012 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.) Jan 24, 2012 at 0:18
  • what's insane about a command behaving consistently regardless of where it's located? Jan 24, 2012 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. Jan 24, 2012 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!). Jan 24, 2012 at 0:30
5

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

readlink -f PATH
1
  • In the specific case readlink -f . would work just fine.
    – mdpc
    Feb 18, 2013 at 18:18
3

From the shell.

pwd -P

From userland.

/bin/pwd
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
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 

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .