Linux – Getting the full path to a file with “readlink -f” does NOT work on OSX

linuxmacos

On Linux (redhat/ubuntu/you name it), I can do this:

$readlink -f myfile.txt
/tmp/local/path/to/myfile.txt

On OSX (yosemite):

$ readlink -f eclipse-projects.xml 
readlink: illegal option -- f

Can someone tell me what's going on?
What's the "equivalent" of -f on osx?
(And why do people love to make things sooooo inconsistent?!)

Best Answer

# Return the canonicalized path (works on OS-X like 'readlink -f' on Linux); . is $PWD
function realpath {
    [ "." = "${1}" ] && n=${PWD} || n=${1}; while nn=$( readlink -n "$n" ); do n=$nn; done; echo "$n"
}
Related Question