0

I've run into situations where symlinks point to other symlinks causing me to have to run multiple ls commands to try to trace where the base file is stored. For example if I want to know the location of java program I run on centos I usually start with /bin/java and have to go 3-4 symlinks deep before I find the actual file's location.

Is there a simple clean command that will trace along all symlinks until it finds a real file and give me the location of the base file?

thanasisp
  • 8,122
dsollen
  • 794
  • 1
  • 13
  • 25
  • 2
    This in not the original file. The sym-link can be created first (before the target). – ctrl-alt-delor Oct 23 '20 at 16:46
  • When I read "original name", I thought you meant exactly the opposite, finding which name was used to open a file given the file you ended up opening. Or finding /bin/java when you're given /opt/javasomething/bin/java. That, of course would be significantly harder or next to impossible. – ilkkachu Oct 23 '20 at 21:44

1 Answers1

5

Use realpath, it will expand all symbolic links until a file target.

For example, for me:

> ls -al /usr/bin/java
lrwxrwxrwx 1 root root 22 Jul 13  2019 /usr/bin/java -> /etc/alternatives/java
> realpath /usr/bin/java
/usr/lib64/jvm/java-11-openjdk-11/bin/java

Also readlink alone would give the direct target, while readlink -f will give the final file:

> readlink /usr/bin/java
/etc/alternatives/java
> readlink -f /usr/bin/java
/usr/lib64/jvm/java-11-openjdk-11/bin/java
thanasisp
  • 8,122