For example, I have a file a, and I do the followings.

ln -s a b
ln -s b c
ln -s c d

If I use readlink d, I will get c. So I have to run many times to get a.

Is there an easy way to get a?


If I want to locate the original file of /usr/bin/java, I have to do it many times to get /usr/lib/jvm/java-7-openjdk-amd64/jre/bin/java. The files and links are not in the same folder.


Command for following chains of links is named nameiManPage. Examples:

$ touch a
$ ln -s a b
$ ln -s b c
$ ln -s c d
$ namei ./d
f: ./d
 d .
 l d -> c
   l c -> b
     l b -> a
       - a
$ namei /usr/bin/java
f: /usr/bin/java
 d /
 d usr
 d bin
 l java -> /etc/alternatives/java
   d /
   d etc
   d alternatives
   l java -> /usr/lib/jvm/java-7-openjdk-i386/jre/bin/java
     d /
     d usr
     d lib
     d jvm
     d java-7-openjdk-i386
     d jre
     d bin
     - java
  • Maybe this is what I am looking for! Thanks a lot! – eccstartup Nov 8 '13 at 3:19

You can use the following shell function:

readmultilink () {
    if [ ! -L "$linkfile" ]; then 
        echo "$linkfile is not a simbolik link" >&2
        return 1
    until [ ! -L "$linkfile" ]; do
        linkfile=$(readlink "$lastlinkfile")
    readlink "$lastlinkfile"

Add this function at the end of your ~/.bashrc file if you want to use it every time when you open the terminal.


readmultilinks file_name

As example, for your example, readmultilinks d, readmultilinks c, and readmultilinks b will return a, but readmultilinks a will return a is not a simbolik link.


A simple solution:

readlink -f /path/to/file

You maybe need find instead:

$ find -L . -samefile d


$ ls -i d
143075 d
$ find . -follow -print -inum 143075

Both cases the original file is the last one.

  • These cases will only locate files in the given folder. – eccstartup Nov 7 '13 at 4:36
  • @eccstartup you only have to change . for the directory you like, ie /, but they will take time. – Braiam Nov 7 '13 at 4:38
  • What is the result on your computer? If the link is /usr/bin/java? – eccstartup Nov 7 '13 at 4:41
  • @eccstartup beats me... I don't have/use java, Also, it will depends what version of java you install, but I gave you a general solution for all sort of problems. – Braiam Nov 7 '13 at 4:46
  • Well, I get many many results. If I redirect the result into a file, the size of the file is over 10MB, and it is increasing because I kill it with Ctrl+C. – eccstartup Nov 7 '13 at 4:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.