Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Rep:
Traverse all symlinks until the file is reached
I know there is a command which enables me to find all symlinks until I found the real file or program.
For example, vi on my system is found like this:
Code:
which vi
/usr/bin/vi
file /usr/bin/vi
/usr/bin/vi: symbolic link to `/etc/alternatives/vi'
file /etc/alternatives/vi
/etc/alternatives/vi: symbolic link to `/usr/bin/vim.basic'
file /usr/bin/vim.basic
/usr/bin/vim.basic: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked (uses shared libs), for GNU/Linux 2.6.26, BuildID[sha1]=0x34857ec79e4fc4cece4ed3a82cae1dff71dc63fe, stripped
But there must be a command which shows me at once all the links until I end up at /usr/bin/vim.basic. I think I even saw it here but for the life of it I can't find it anymore. Don't try to google for "file symlink follow"
Only problem is when there is a symbolic link loop... But for most things that won't happen
True. It's also misleading if $1 doesn't exist (no link, no file, ... nothing). It just echoes back $1 as though it found an ordinary file there. That's an easy fix. Detecting the loop would be somewhat more involved.
Hmmm, it also mishandles relative links -- tries to evaluate them relative to the current directory instead of the directory where the link resides. A bit more complicated now:
Code:
#!/bin/sh
# Show the chain of symlinks to a target
stat "$1" >/dev/null || exit
Y="$1"
X="${1##*/}"
Dir="${1%$X}"
while [ -L "$Dir$X" ]; do
echo -n "$Y => "
Y="$(readlink "$Dir$X")"
X="${Y##*/}"
case "$Y" in
/*)
Dir="${Y%$X}";;
*)
Dir="$Dir${Y%$X}";;
esac
done
echo "$Y"
Last edited by rknichols; 10-13-2014 at 12:03 AM.
Reason: add Hmmm, ...
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Original Poster
Rep:
Thanks all. I assumed it could be scripted somehow, but knowing the command is easier so it is always avaialable even on machines not connected to my network. I keep hoping.
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195
Original Poster
Rep:
Well, what I'd like to see is the output you produced with your script. Which is very nice btw. However, if you regularly work on many different machine you know what the problem is managing you collection of useful scripts. They reside at your own network, and not at the host you are currently working at.
I am frustrated because a few weeks ago I saw this command in one of the threads here and I forgot the name.
Unfortunately readlink doesn't do what I want. readlink /sbin/iptables produces iptables which is helpful. readlink vi doesn't produce any output while it would be interesting to see where vi points to.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.