Unix list absolute file name

Posted by Matthew Adams on Super User See other posts from Super User or by Matthew Adams
Published on 2012-11-27T20:45:13Z Indexed on 2012/11/28 5:08 UTC
Read the original article Hit count: 612

Filed under:
|
|
|

Given an arbitrary single argument representing a file (or directory, device, etc), how do I get the absolute path of the argument?

I've seen many answers to this question involving find/ls/stat/readlink and $PWD, but none that suits my need. It looks like the closest answer is ksh's "whence" command, but I need it to work in sh/bash.

Assume a file, foo.txt, is located in my home directory, /Users/matthew/foo.txt. I need the following behavior, despite what my current working directory is (I'm calling the command "abs"):

(PWD is ~)
$ abs foo.txt
/Users/matthew/foo.txt

$ abs ~/foo.txt
/Users/matthew/foo.txt

$ abs ./foo.txt
/Users/matthew/foo.txt

$ abs /Users/matthew/foo.txt
/Users/matthew/foo.txt

What would "abs" really be?

TIA, Matthew

© Super User or respective owner

Related posts about bash

Related posts about unix