1

How do I obtain a description of all encountered symlinks for the given path?

Expecting output like this:

$ linksinfo ~/.nix-profile/lib/libQt5OpenGL.so
/home is a directory
/home/nix is a directory
/home/nix/.nix-profile -> /home/nix/.nix-profile -> /nix/var/nix/profiles/default
/nix/var/nix/profiles/default/lib is a directory
/nix/var/nix/profiles/default/lib/libQt5OpenGL.so -> /nix/store/33xkmx1f1040s5nb15x7hx2cqmyw1jyi-qt-5.3.1/lib/libQt5OpenGL.so
/nix/store/33xkmx1f1040s5nb15x7hx2cqmyw1jyi-qt-5.3.1/lib/libQt5OpenGL.so -> libQt5OpenGL.so.5.3.1
/nix/store/33xkmx1f1040s5nb15x7hx2cqmyw1jyi-qt-5.3.1/lib/libQt5OpenGL.so.5.3.1 is a file
Vi.
  • 5,688
  • Can you please explain your output a bit more. It doesn't make total sense. You're giving linksinfo a path to a file, but what do you expect it to do then? – slm Oct 01 '14 at 00:33
  • Scan all components of the path (in this case, /, home, nix, .nix-profile, lib, libQt5OpenGL.so) and output destinations of all encountered symlinks, including when them does "chain". It should provide answer to the question "How many symbolic links get dereferenced when I access this path". – Vi. Oct 01 '14 at 00:56
  • Is this for a school project? It's fine if it is, but pls state it as such and explain where you're stuck. – slm Oct 01 '14 at 01:00
  • @slm, No, I'm just looking for a tool that makes navigating in a labyrinth of symlinks (in Nix they are used heavily) simpler, without manual ls -ld. Before writing a tool myself I often "lookup" it using StackExchange. – Vi. Oct 01 '14 at 01:12
  • @not a problem, just had to ask. There isn't a tool that will do this directly. The closest tool to doing anything related to this is called readlink. – slm Oct 01 '14 at 01:20
  • @slm, It process only the final link. Also in case of chained link it shows only the final destination. – Vi. Oct 01 '14 at 01:28
  • That's correct. As I said it's the only tool that I'm aware of that does any evaluations w/ links. – slm Oct 01 '14 at 01:31
  • There's also the tool symlink as I've discussed it here: http://unix.stackexchange.com/questions/99159/is-there-an-algorithm-to-decide-if-a-symlink-loops/99166#99166 – slm Oct 01 '14 at 02:22

1 Answers1

1

Lets create simple script:

#!/bin/bash            

mypath=$1
while [[ "${#mypath}" -gt 1 ]]; do
      file "$mypath"
      mypath="$(dirname $mypath)"
done

Test:

$ ./linksinfo /usr/src/linux/kernel/../../../../bin/sh
/usr/src/linux/kernel/../../../../bin/sh: symbolic link to `bash'
/usr/src/linux/kernel/../../../../bin: directory
/usr/src/linux/kernel/../../../..: directory
/usr/src/linux/kernel/../../..: directory
/usr/src/linux/kernel/../..: directory
/usr/src/linux/kernel/..: directory
/usr/src/linux/kernel: directory
/usr/src/linux: symbolic link to `linux-3.14.14-gentoo'
/usr/src: directory
/usr: directory

Edit

This does not check/show all path components, but the output in you question doesn't do this either. For example after

/home/nix/.nix-profile -> /nix/var/nix/profiles/default

it is possible that any of /nix, /nix/var/, /nix/var/nix, /nix/var/nix/profiles or /nix/var/nix/profiles/default is a link itself. However your output skips this and checks only /nix/var/nix/profiles/default/lib. Similar thing happens with

/nix/var/nix/profiles/default/lib/libQt5OpenGL.so -> /nix/store/33xkmx1f1040s5nb15x7hx2cqmyw1jyi-qt-5.3.1/lib/libQt5OpenGL.so

Again the output says nothing about /nix/store and /nix/store/33xkmx1f1040s5nb15x7hx2cqmyw1jyi-qt-5.3.1. Long story short I suggest to reconsider the problem and think what you really want to achieve. Perhaps simple readlink [-f] or realpath [-e] is enough?

jimmij
  • 47,140
  • The question is about reporting symbolic links, which your script doesn't do except when they're a component of the initial path. – Gilles 'SO- stop being evil' Sep 30 '14 at 23:20
  • @Gilles See the edit of my answer. I agree that this doesn't report about all symbolic links, but OP didn't specify exactly which path components should be followed. I think my answer solves at least part of the problem. – jimmij Oct 02 '14 at 13:25