如何知道linux中符号链接的级别?

Xiè*_*léi 3 linux symbolic-link

例如,如果一个符号链接

a -> b
b -> c
c -> d
Run Code Online (Sandbox Code Playgroud)

比如说,a 的符号链接级别是 3。

那么,是否有任何实用程序可以获取此信息?而且,我还想获得符号链接的扩展细节,它会显示如下内容:

1. /abc/xyz is expanded to /abc/xy/z (lrwx--x--x root root)
2. /abc/xy/z is expanded to /abc/xy-1.3.2/z (lrwx--x--x root root)
3. /abc/xy-1.3.2/z is expanded to /abc/xy-1.3.2/z-4.6 (lrwx--x--x root root)
4. /abc/xy-1.3.2/z-4.6 is expanded to /storage/121/43/z_4_6 (lrwx--x--x root root)
5. /storage/121/43/z_4_6 is expanded to /media/kitty_3135/43/z_4_6 (lrwx--x--x root root)
Run Code Online (Sandbox Code Playgroud)

所以我可以用符号链接进行诊断。任何的想法?

Den*_*son 5

此递归 Bash 函数将打印链接链和计数以及诊断信息:

chain() { local link target; if [[ -z $_chain ]]; then unset _chain_count _expansion; _chain="$1"; fi; link=$(stat --printf=%N $1); while [[ $link =~ \-\> ]]; do target="${link##*\`}"; target="${target%\'}"; _chain+=" -> $target"; ((_chain_count++)); _expansion+="$_chain_count. $1 is expanded to $target $(stat --printf="(%A %U %G)" $target)"$'\n'; chain "$target"; return; done; echo "$_chain ($_chain_count)"; echo "$_expansion"; unset _chain _chain_count _expansion; }
Run Code Online (Sandbox Code Playgroud)

它需要stat. 有关更多信息和使用readlink代替 的版本stat,请在此处查看我的答案(需要添加计数功能,但添加权限和所有者/组会更具挑战性)。

为了这:

a
b -> a
c -> b
d -> c
Run Code Online (Sandbox Code Playgroud)

的输出chain d将是:

d -> c -> b -> a (3)
1. d is expanded to c (lrwxrwxrwx username groupname)
2. c is expanded to b (lrwxrwxrwx username groupname)
3. b is expanded to a (-rw-r--r-- root root)
Run Code Online (Sandbox Code Playgroud)

这是该函数的可读性更强的版本:

chain ()
{
    local link target;
    if [[ -z $_chain ]]; then
        unset _chain_count _expansion;
        _chain="$1";
    fi;
    link=$(stat --printf=%N $1);
    while [[ $link =~ \-\> ]]; do
        target="${link##*\`}";
        target="${target%\'}";
        _chain+=" -> $target";
        ((_chain_count++));
        _expansion+="$_chain_count. $1 is expanded to $target $(stat --printf="(%A %U %G)" $target)"$'\n';
        chain "$target";
        return;
    done;
    echo "$_chain ($_chain_count)";
    echo "$_expansion";
    unset _chain _chain_count _expansion
}
Run Code Online (Sandbox Code Playgroud)