Canonical path without resolve symbol link

I have a symlink file like this,

cd /tmp
ln -s /bin/ls /test

Now I would like to resolve ../test to /test, but the readlink command also resolves the symbol link for me, e.g

$ readlink -f ../test
../test -> /bin/ls

Is there any command that would output /test instead?

Asked By: daisy

||

realpath from GNU coreutils seems to work for me with the -s option:

$ cd /tmp/foo
$ ln -s /bin/ls ../linktest
$ realpath -s ../linktest 
/tmp/linktest

From the GNU coreutils manual: The -s option is:

-s, --strip, --no-symlinks

Do not resolve symbolic links. (…)

Answered By: ilkkachu
Categories: Answers Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.