1

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?

1 Answer 1

5

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. (...)

4
  • @AsukaMinato, the backticks do mark a code block the same as the indentation does, so better remove the indentation when swithing to backticks to avoid the actual data from being shown unnecessarily indented. (I didn't even notice that from the review, blah.) Commented Apr 24, 2023 at 14:53
  • To solve all the symlinks but the last, I use slink='/path/with/symlinks/TheSymlink'; echo $(realpath $(dirname $slink))/$(basename $slink), which prints /canonical/path/to/TheSymlink Commented Jul 23, 2024 at 16:53
  • 1
    @alexis, though you'd want to remember to double-quote everything there Commented Jul 23, 2024 at 18:41
  • Right!, thanks: echo "$(realpath "$(dirname "$slink")")"/"$(basename "$slink")" Commented Aug 16, 2024 at 14:26

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.