views:

191

answers:

2

Hi all,

Is there a bash command that takes as input a file path and returns an absolute file path? More specifically I would like a command that takes as input a path such as:

/tmp/yaneeve/kit/linux/../../output/kit/SOURCES//usr//apps/myapp/lib

and returns the path:

/tmp/yaneeve/output/kit/SOURCES/usr/apps/myapp/lib

Thanks!

+4  A: 

Try (Linux)

readlink -f
TheBonsai
Hi all,Notice that I've found a bug (?) in readlink. When the path does not really exist it appends only the first part to its output.For example:say the input is /tmp/yaneeve/apps/newapp/guide/engand say that the deepest part of this path that exists is /tmp/yaneeve/appsthe output would be /tmp/yaneeve/apps/newappAny comments on this finding?
Yaneeve
It doesn't output anything here, for nonexisting pathes (I used exactly your example).
TheBonsai
+3  A: 

If the path exists, there is a portable way which is (even on linux) far more reliable:

canonicalPath=$(cd "$path"; pwd)
lhunath
Good catch. Is there any need to use the external pwd program? The internal one follows the logical path, which diesn't need to match the "physical" one.
TheBonsai
how is it more reliable?
Yaneeve
mkdir a "dir1", create a link "dir2" to it, cd dir2, compare pwd and command pwd.
TheBonsai