Issue
What would be an elegant way of comparing 2 symbolic links in Unix?
Assume I have entered the following commands:
ln -s /usr/share/g s1
ln -s /usr/share/g s2
ln -s ../share/g /usr/lib/s3
ln -s /usr/share/h s4
then I would like to have a command that says that s1 and s2 are equal (whether /usr/share/g actually exists is not relevant), that s1 and s4 are not equal, s2 and s4 are not equal. (For my purpose it suffices that s3 is reported to differ from s1 and s2; however if comparison after path normalization could be done, that may be useful to.)
Solution
For GNU systems (and possibly others, but I can't say), there's readlink(1)
:
$ touch a
$ ln -s a b
$ readlink b
a
You can use that in comparisons:
$ test "$(readlink -f a)" = "$(readlink -f b)"
$ echo $?
0
Answered By - Torsten Marek Answer Checked By - David Goodson (WPSolving Volunteer)