Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

On Linux, if I've cd'd around and am now in a directory, is there a way to figure out what the real path to that directory is if I had not used a symbolic link to get there?

Consider:

$ pwd
/home/dave/tmp
$ mkdir -p 1/2/3/4/5
$ ln -s 1/2/3/4/5 5
$ cd 5
$ pwd 
/home/dave/tmp/5

Or:

$ pwd
/home/dave/tmp
$ mkdir -p 1/2/3/4/5
$ ln -s 1/2/3/4 4
$ cd 4/5
$ pwd 
/home/dave/tmp/4/5

Is there any way to figure out that /home/dave/tmp/5 is really /home/dave/1/2/3/4/5 ?

share|improve this question

3 Answers 3

up vote 13 down vote accepted

For use with cd, use pwd -P:

$ pwd
/home/dave/tmp
$ mkdir -p 1/2/3/4/5
$ ln -s 1/2/3/4/5 5
$ cd 5
$ pwd 
/home/dave/tmp/5
$ pwd -P
/home/dave/tmp/1/2/3/4/5

For generic symbolic links, use readlink:

$ cd ..
$ readlink 5
1/2/3/4/5

Or ls -l (with -d for directories):

$ ls -ld 5
lrwxr-xr-x  1 dave  staff  9 Jul 24 10:10 5 -> 1/2/3/4/5
share|improve this answer
    
readlink -f file returns the non-symlinked path to that file, which is what I want. Thanks! –  David Mackintosh Jul 27 '09 at 13:40
1  
huh, I'd always used /bin/pwd, since it can't know how it got where it is, unlike the pwd shell builtin. Obviously equivalent to pwd -P. –  Peter Cordes Dec 10 '09 at 20:00
    
ls -lda doesn't work for hidden sym links for some reason –  Casebash Jun 11 '10 at 12:32

An alternative method I used,

cd to the symlink
Ctrl+Shift+T #To open a new tab
pwd

New tab pwd, gives the symlink. ;)

share|improve this answer
    
I'm sorry, what shell, window manager, or terminal are you using here? –  David Mackintosh Aug 22 '09 at 18:06
    
I think he's talking about gnome-terminal –  prestomation Nov 6 '09 at 22:01
    
Yes, I was talking about the Gnome Terminal. –  Lakshman Prasad Nov 10 '09 at 6:29

You want either readlink -f (in coreutils, installed by default) or the more easily-remembered realpath, which you have to install.

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.