Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Suppose I have a link that points to another link that points to another link, etc. way down the line. I'm not sure how many levels of links there are. What's the slick command line way to see the ultimate target of all these links?

share|improve this question
add comment

5 Answers

up vote 6 down vote accepted

Some versions of readlink support the -f option to follow the chain to the end. The one on my Debian box does. The one on my Mac OS box does not (but then, it is overridden by one supplied by fink which does).

share|improve this answer
add comment

Maybe it's not the best solution, it depends of your needs. You can retrieve the inode of the end of the chain with:

stat -Lc '%i' the-link

and if you need the path of those files (more than one if the ends of the chain are hard-linked each other) too:

find /search/path/ `stat -Lc '%i' the-link`

Ok, there's a smarter solution:

readlink -f the-file
share|improve this answer
add comment

See my answers here and here for small functions that you can use as starting points to accomplish what you want if you want to see all the intermediate links.

share|improve this answer
add comment

Take a look at this perl script, I think it's what you're looking for.

ftp://ftp.cs.duke.edu/pub/yuji/tools/reslink/reslink.pl

share|improve this answer
add comment

If you want a more cross-platform solution you can use:

#!/usr/bin/env bash
s=/path/to/file
while [ -h $s ]; do s="$(readlink "$s")"; done
echo $s

Seems inelegant but, as stated by dmckee, Mac OS X doesn’t have the -f option on readlink.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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