How can I derefence symbolic links in bash?


Question

How can I take any given path in bash and convert it to it's canonical form, dereferencing any symbolic links that may be contained within the path?

For example:

~$ mkdir /tmp/symtest
~$ cd /tmp/symtest/
/tmp/symtest$ mkdir -p foo/bar cat/dog
/tmp/symtest$ cd foo/bar/
/tmp/symtest/foo/bar$ ln -s ../../cat cat
/tmp/symtest/foo/bat$ cd ../../
/tmp/symtest$ tree
.
|-- cat
|   `-- dog
`-- foo
    `-- bar
       `-- cat -> ../../cat

6 directories, 0 files

How can I get the full canonical path of /tmp/symtest/foo/bar/cat (i.e: /tmp/symtest/cat)?

1
41
7/29/2009 1:46:48 AM

Accepted Answer

Thanks to Andy Skelton, it appears the answer is readlink -f:

$:/tmp/symtest$ readlink -f /tmp/symtest/foo/bar/cat
/tmp/symtest/cat
58
7/29/2009 1:48:55 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon