Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

If I do pwd I notice it uses whatever symlinks I used to get into the current directory. Can I get it to tell me the "real" directory I'm in ... i.e. the path from the root to my current directory without the use of any symlinks?

share|improve this question
add comment

2 Answers 2

According to the POSIX manpage for pwd, the -P option may be of use:

-P The absolute pathname written shall not contain filenames that, in the context of the pathname, refer to files of type symbolic link.

Thus

$ pwd -P

should be what you need.

share|improve this answer
add comment

The pwd shell built-in uses the path the shell keeps track of when you cd (and stores it in $PWD). This means if you have a symlink to a complex (deep) path, it will tell you what you typed to change to that directory instead of the real path. This is done to give you what you want most of the time.

/bin/pwd uses the getcwd system call (which these days is a library call, reading /proc/self/cwd) which returns the canonical path for the current directory, sans all symlink traversals.

As Steven D pointed out, pwd has the -P option to ignore $PWD. It also has the -L option to return the contents of $PWD. The man page for pwd does not say which option is used by default but experience tells me the above description is correct (shell pwd vs. /bin/pwd). However you should probably not rely on that and just use pwd -P.

share|improve this answer
1  
The behavior you describe is the one on Linux systems where /bin/pwd is GNU pwd when POSIXLY_CORRECT is not set. On POSIX systems where the pwd is in /bin. /bin/pwd will use $PWD and may return paths with symlinks. GNU pwd will behave in that POSIX manner when it find a POSIXLY_CORRECT variable in its environment. –  Stéphane Chazelas Dec 2 '12 at 20:10
add comment

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.