Given this example:
mkdir a
ln -s a b
ln -s b c
ln -s c d
If I execute:
ls -l d
It will show:
d -> c
Is there a way for ls
or any other linux command to show d -> c -> b -> a
instead?
Given this example:
mkdir a
ln -s a b
ln -s b c
ln -s c d
If I execute:
ls -l d
It will show:
d -> c
Is there a way for ls
or any other linux command to show d -> c -> b -> a
instead?
Just use
namei
:readlink -e <link>
note: readlink a by itself returns b
note #2: together with find -l, a utility to list the chains could easily be written in perl, but also has to be smart enough to detect loops
readlink will not output anything if you have a loop. This is better than getting stuck, I suppose.
Here is a recursive function in Bash:
On multiple lines:
Examples:
It requires
stat(1)
which may not be present on some systems.It will fail if names contain backticks, single quotes, or "->". It gets stuck in a loop with symlink loops (this could be solved using an associative array in Bash 4). It exports a variable called "chain" without regard to whether it's already in use.
There may be other problems with it.
Edit:
Fixed a problem with some relative symlinks. Some still don't work, but the version below doesn't require the target of the link to exist.
Added a version that uses readlink:
You could just postprocess the output of namei with something like
awk
orgrep
to get just the lines you want:or