2

I want to resolve a symbolic link with the additional option to print the whole linkage. For example I want to run a command like readlink -f "symlink2" which reports each entry of the linkage. In other words if you would resolve the symbolic link "symlink2->symlink1->/path/file" then the result of command above is "/path/file" and I am lokking for a output like:

$readlink -f "symlink2
symlink1
/path/file
Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Hölderlin
  • 1,160
  • 5
  • 14
  • 34
  • Possible duplicate of http://stackoverflow.com/questions/16017500/how-to-see-full-symlink-path. If this isn't a duplicate, can you edit or rephrase your question to explain why `readlink -f symlinkName` doesn't work for you to display the real path of the symlink. – Christia Dec 27 '16 at 21:22
  • @Christia, this information you like to know should already there. But thx for your evidence, I edited my question to clarify my question. – Hölderlin Dec 27 '16 at 21:36

2 Answers2

3

One way to do it is to make your own recursive readlink.

function readlinkWithPrint() {
    link=`readlink "$@"`
    [ -e "$link" ] && echo "$link"
    [ -h "$link" ] && readlinkWithPrint "$link"
}

Here's a test:

$ touch file
$ ln -s file symlink1
$ ln -s symlink1 symlink2
$ readlinkWithPrint symlink2
symlink1
file

Unfortunately, this function is quite basic; the options you provide to frist readlink will not propagate to the rest plus you cannot read multiple files.

ihato
  • 285
  • 2
  • 6
2

I think you're looking for namei, which is part of the util-linux suite, available on every non-embedded Linux system.

See this thread regarding an equivalent for macOS. The source code should be mostly portable if you need it on some other Unix variant.

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175