3

I sometimes want to know where an original/real file is on my system that is linked via multiple symlinks to a known handle. Running ls -l /path/to/file will show me the location of the next link, but if I want to track down the original then I find myself copy-pasting a bunch of paths, which is cumbersome.

Is there a more direct way (viz. single shortish command) to go straight from A to Z? Or do I have to build a function that sequentially works through each link till we reach the final 'real' file? (Suggestions for such a function are welcome!)

Example case: I wanted to find all of the postgresql executables installed together on centos. To do that, I had to burrow down the following rabbit hole:

which psql
> /usr/bin/psql
ls -l /usr/bin/psql 
> l.......... ... /usr/bin/psql -> /etc/alternatives/pgsql-psql*
ls -l /etc/alternatives/pgsql-psql
> l.......... ...  /etc/alternatives/pgsql-psql -> /usr/pgsql-11/bin/psql*
ls -l /usr/pgsql-11/bin/psql
> -.......... ... /usr/pgsql-11/bin/psql*
ls /usr/pgsql-11/bin/
> [Voila!]

Is there an easier (and generalizable) way to get from psql -> /usr/pgsql-11/bin/?

1
  • Similar question here, with comments linking to more similar questions. One solution is to install GNU realpath via brew install coreutils, another to write a small wrapper program around the realpath syscall. Commented Nov 14, 2022 at 12:56

1 Answer 1

4

You can use realpath to recursively resolve all symlinks and give you a canonical path:

$ cd /tmp
$ touch baz
$ ln -s baz bar
$ ln -s bar foo

$ ls -l foo
lrwxrwxrwx 1 me me 3 Nov 20 09:42 foo -> bar

$ realpath foo
/tmp/baz
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.