Within a bash script, I know I can check if a file is a symbolic link with the following syntax
if [ -L $path ]
Does any one know how I would test if that path was linked to a particular path? E.g. I want to check whether the target of $path
is /some/where
.
Best Answer
If you want to check whether
$path
is a symbolic link whose target is/some/where
, you can use thereadlink
utility. It isn't POSIX, but it's available on many systems (GNU/Linux, BusyBox, *BSD, …).Note that this is an exact text comparison. If the target of the link is
/some//where
, or if it'swhere
and the value of$path
is/some/link
, then the texts won't match.Many versions of
readlink
support the option-f
, which canonicalizes the path by expanding all symbolic links.Many shells, including dash, ksh, bash and zsh, support the
-ef
operator in thetest
builtin to test whether two files are the same (hard links to the same file, after following symbolic links). This feature is also widely supported but not POSIX.