knocte
knocte

Reputation: 17949

Transform path with ".." elements to absolute path, in a script?

So when coding a bash/sh script I got a path in a variable with the following content:

$SOMEPATH=/usr/lib64/pkgconfig/../..

How can I convert $SOMEVAR to a full absolute path that converts the ".." folders to this value?:

$SOMEFULLPATH=/usr

Thanks

Upvotes: 0

Views: 67

Answers (2)

TTT
TTT

Reputation: 1205

Similar to the answer from @devnull I would recommend readlink, however the -m option does not require any part of the file path to already exist, which is more flexible.
readlink -m $SOMEFULLPATH

Upvotes: 1

devnull
devnull

Reputation: 123568

You could use readlink:

readlink -f "$SOMEPATH"
   -f, --canonicalize
          canonicalize  by  following  every symlink in every component of
          the given name recursively; all  but  the  last  component  must
          exist

   -m, --canonicalize-missing
          canonicalize by following every symlink in  every  component  of
          the  given  name recursively, without requirements on components
          existence

Upvotes: 4

Related Questions