Getting real path to directory

Hi friends,

Need your help. The question is:
1) We have directory '/SomeDirectory/SubDirectory'
2) And have symlink pointing to it like '/AnotherOneDirectory/symlink'
'/AnotherOneDirectory/symlink' -> '/SomeDirectory/SubDirectory'

3) Need to get real path without symlinks knowing only '/AnotherOneDirectory/symlink'

4) P.S. Using shell commands only. Something like realpath() call for C.

Thanks, kandrewo.

Is this a homework?
Please read the forum rules!

What are you talking about? It isn't homework!!!
It is issue I found during my work with shell. If you have no idea how to resolve it I understand you, this is not trivial task, as it can appear at the first look.

We can use find, pwd and other tools, but they don't provide us with real path. Unfortunately :frowning:

Thanks, kandrewo.

This statement makes me think it's a homework:

Why only shell commands?

ls does it for me

admins-imac:~ tlarkin$ ls -al /etc
lrwxr-xr-x@ 1 root  admin  11 Sep  2 13:34 /etc -> private/etc
admins-imac:~ tlarkin$ 

bolded shows the parent folder. I am not using standard Unix though, I am using OS X. Might be some differences.

Your question made be think about the real reason... Hm...
I found that I don't know how to retrieve full real path in shell and it confused me. Of course, I can write something like

char * szResult = realpath(sPath.c_str(), szResolvedPath);

in C++ program and use it. But it is not a solution for each system, where our additional binary is missed. Say at my friend home :slight_smile:

---------- Post updated at 09:36 PM ---------- Previous update was at 09:29 PM ----------

So the 1st way I can see is to use something like

ls -l "directoryname" | cut -f2 -d '>' | sed 's/^ //'

It isn't graceful way, don't you think so? As for me: it is usable but depends on ls standard, and possible differences between UNIX systems. Doesn't it?

Some hints:

not portable: realpath, readlink -f, somehow GNU find printf '%l\n'
portable: Perl has readlink, POSIX shell (cd -P <path> && pwd -P)

Also see:

www.stonehenge.com/merlyn/UnixReview/col27.html 
groups.google.com/group/comp.unix.shell/msg/9c4b26b1b49a2ead

Thanks a lot.

'pwd -P' works.

It is strange that there are no description of '-P' argument in pwd man page.
I tried on SunOS.
Maybe this is because shell has built-in pwd.

Think, this topic should be closed.

Thanks, kandrewo.