如何显示给定路径中所有遇到的符号链接的信息?

如何显示给定路径中所有遇到的符号链接的信息?

如何获取给定路径中所有遇到的符号链接的描述?

期待这样的输出:

$ 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

答案1

让我们创建简单的脚本:

#!/bin/bash            

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

测试:

$ ./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

编辑

这不会检查/显示所有路径组件,但您问题中的输出也不会执行此操作。例如之后

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

/nix/nix/var//nix/var/nix或中的任何一个本身/nix/var/nix/profiles都可能/nix/var/nix/profiles/default是链接。但是,您的输出会跳过此步骤并仅检查/nix/var/nix/profiles/default/lib.类似的事情发生在

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

输出再次没有提及/nix/store/nix/store/33xkmx1f1040s5nb15x7hx2cqmyw1jyi-qt-5.3.1。长话短说,我建议重新考虑这个问题并思考你真正想要实现的目标。也许很简单readlink [-f]或者realpath [-e]就足够了?

相关内容