LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
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


Reply
  Search this Thread
Old 10-12-2014, 08:30 PM   #1
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
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
 
Old 10-12-2014, 09:17 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,804

Rep: Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224
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
 
Old 10-12-2014, 09:45 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,317

Rep: Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172Reputation: 4172
realpath ?.
 
Old 10-12-2014, 10:16 PM   #4
jpollard
Senior Member
 
Registered: Dec 2012
Location: Washington DC area
Distribution: Fedora, CentOS, Slackware
Posts: 4,908

Rep: Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513Reputation: 1513
Quote:
Originally Posted by rknichols View Post
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
 
Old 10-12-2014, 11:57 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,804

Rep: Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224
Quote:
Originally Posted by jpollard View Post
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, ...
 
Old 10-13-2014, 09:08 AM   #6
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Original Poster
Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
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
 
Old 10-13-2014, 10:25 AM   #7
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,804

Rep: Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224
I thought you wanted to see all the intermediate steps. AFAIK, readlink doesn't do that.
 
Old 10-13-2014, 01:49 PM   #8
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Original Poster
Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
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
 
Old 10-13-2014, 04:15 PM   #9
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,804

Rep: Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224Reputation: 2224
Code:
readlink `which vi`
 
1 members found this post helpful.
Old 10-13-2014, 05:29 PM   #10
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,196

Original Poster
Rep: Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044Reputation: 1044
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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Traverse file structure from top and rename the immediate parent sahil.jammu Linux - General 7 05-31-2011 03:09 AM
Traverse the file system and Rename (xargs or sed?) sahil.jammu Linux - General 19 05-25-2011 11:15 AM
Script to traverse into sub-directory and replace line of file inside zip Marshalle Linux - General 2 08-28-2010 12:38 AM
Trying to traverse a directory to change file permissions bskrakes Linux - Newbie 4 09-14-2006 05:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 09:19 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration