Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

canonicalize a path name on solaris

On a GNU system I would just use readlink -f $SOME_PATH, but Solaris doesn't have readlink.

I'd prefer something that works well in bash, but other programs are ok if needed.

Edit: The best I've come up with so far uses cd and pwd, but requires some more hackery to deal with files and not just directories.

cd -P "$*"
REAL_PATH=`pwd`
like image 901
A B Avatar asked Dec 18 '22 06:12

A B


2 Answers

Might be overkill, but this is OS portable, and does not need to find the dirname nor basename binaries first.. this one-liner works. Just pass in your filename where you see $origFile:

perl -e "use Cwd realpath; print realpath(\"$origFile\");"

like image 50
Yavin5 Avatar answered Dec 26 '22 14:12

Yavin5


Does this help? From the referenced page:

Create a file called canonicalize with these contents:

#!/bin/bash
cd -P -- "$(dirname -- "$1")" &&
printf '%s\n' "$(pwd -P)/$(basename -- "$1")"

Make the file executable:

chmod +x canonicalize`

And finally:

user@host$ canonicalize ./bash_profile
like image 33
Sean Bright Avatar answered Dec 26 '22 16:12

Sean Bright