Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Absolutize possibly non-existent path in bash? (linux)

Tags:

linux

bash

path

There are many instances on SO where people have asked how best to absolutize a path in bash, but I haven't found any that deal with absolutizing non-yet-existent paths.

The cd $1 && pwd method does not work at all for non-existent paths.

The readlink -f $1 method will only work for one level of non-existence; that is, it will work for "~/sdkjf", but not for "~/sdkjf/qweuio"

Ideally, I would like a method that would expand the latter to /home/username/sdkjf/qweuio ... and would also do similar transforms for ./ and ../

Can you recommend a solution?

like image 793
Ryan V. Bissell Avatar asked Jun 06 '26 17:06

Ryan V. Bissell


1 Answers

readlink -m $1

From the manpage:

  -m, --canonicalize-missing
          canonicalize  by  following  every  symlink  in every component of the given 
          name recursively, without requirements on components existence
like image 108
Ryan V. Bissell Avatar answered Jun 08 '26 07:06

Ryan V. Bissell



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!