Figure out what non-symlink path would be?

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 ?


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

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