I'm looking for a simple command that can be used within Bash to find the absolute and canonicalized path to a file on an OS X (similar to “readlink -f'` under Linux).
The following sample bash session describes a [fictitious] utility called “abspath'` that exhibits the desired behavior:
$ pwd
/Users/guyfleegman
$ ls -lR
drwxr-xr-x 4 guyfleegman crew 136 Oct 30 02:09 foo
./foo:
-rw-r--r-- 1 guyfleegman crew 0 Oct 30 02:07 bar.txt
lrwxr-xr-x 1 guyfleegman crew 7 Oct 30 02:09 baz.txt -> bar.txt
$ abspath .
/Users/guyfleegman
$ abspath foo
/Users/guyfleegman/foo
$ abspath ./foo/bar.txt
/Users/guyfleegman/foo/bar.txt
$ abspath foo/baz.txt
/Users/guyfleegman/foo/baz.txt
As with the last invocation of “abspath'` in the above example, I'd prefer it didn't automatically resolve symlinks, but I'm not going to be too picky here.
Best Answer
Examples: