Linux - Software This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
|
10-12-2014, 08:30 PM
|
#1
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
|
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"
jlinkels
|
|
|
10-12-2014, 09:17 PM
|
#2
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,804
|
I vaguely recall seeing something like that before, but it's pretty easy to write a script:
Code:
#!/bin/sh
# Show the chain of symlinks to a target
X="$1"
while [ -L "$X" ]; do
echo -n "$X => "
X="$(readlink "$X")"
done
echo "$X"
Sample output:
Code:
$ showlink /sbin/iptables
/sbin/iptables => /etc/alternatives/iptables.x86_64 => /sbin/iptables-1.4.7 => iptables-multi
|
|
|
10-12-2014, 09:45 PM
|
#3
|
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,317
|
realpath ?.
|
|
|
10-12-2014, 10:16 PM
|
#4
|
Senior Member
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,908
|
Quote:
Originally Posted by rknichols
I vaguely recall seeing something like that before, but it's pretty easy to write a script:
Code:
#!/bin/sh
# Show the chain of symlinks to a target
X="$1"
while [ -L "$X" ]; do
echo -n "$X => "
X="$(readlink "$X")"
done
echo "$X"
Sample output:
Code:
$ showlink /sbin/iptables
/sbin/iptables => /etc/alternatives/iptables.x86_64 => /sbin/iptables-1.4.7 => iptables-multi
|
Only problem is when there is a symbolic link loop... But for most things that won't happen
|
|
|
10-12-2014, 11:57 PM
|
#5
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,804
|
Quote:
Originally Posted by jpollard
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 01:03 AM.
Reason: add Hmmm, ...
|
|
|
10-13-2014, 09:08 AM
|
#6
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
Original Poster
|
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.
jlinkels
|
|
|
10-13-2014, 10:25 AM
|
#7
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,804
|
I thought you wanted to see all the intermediate steps. AFAIK, readlink doesn't do that.
|
|
|
10-13-2014, 01:49 PM
|
#8
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
Original Poster
|
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.
jlinkels
|
|
|
10-13-2014, 04:15 PM
|
#9
|
Senior Member
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,804
|
Code:
readlink `which vi`
|
|
1 members found this post helpful.
|
10-13-2014, 05:29 PM
|
#10
|
LQ Guru
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196
Original Poster
|
Code:
readlink -f `which vi`
gives the correct output.
Still I like the output of your script better.
But thanks for the pointer anyway.
jlinkels
|
|
|
All times are GMT -5. The time now is 09:19 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|