Wednesday, January 28, 2009

Absolute path resolver

I was looked for some utility to resolve absolute path of file or directory. I found namei but it result returns in form
$ namei /etc/gdc
f: /etc/gdc
d /
d etc
l gdc -> /home/hynek/.gdc
d /
d home
d hynek
l .gdc -> work/tiger/bear/common/etc/gdc
  d work
  d tiger
  l bear -> bear.trunk/
    d bear.trunk
  d common
  d etc
  d gdc
but I suspected something more like
$ abs_path /etc/gdc
/home/hynek/work/tiger/bear.trunk/common/etc/gdc
I haven't found anything better than make alias in my ~/.bashrc
alias abs_path='perl -MCwd -le'\''print Cwd::abs_path($_) foreach @ARGV'\'

Edit: As ZD notice readlink -f does same work. Thanks. readlink -m and readlink -e works as well but differs if some parts missing. I have decided to use

alias abs_path='readlink -m'

2 comments:

ZD said...

How about the simple readlink -f ?

Pichi said...

@ZD: Yep, you've got it.