1

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
  • Possible duplicate of stackoverflow.com/questions/16017500/…. 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

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.

  • Thx for that code snippet. It seems to define a own function in ~/.bashrc is the only way. – Hölderlin Dec 27 '16 at 23:27
1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.