3

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
2
  • 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, 2016 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, 2016 at 21:36

2 Answers 2

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.

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

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.

1
  • this is exactly what I am looking for! thank you so much :-)
    – asgs
    Oct 11, 2023 at 12:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .