How to verify the target of a symbolic link points toward a particular path

19,765

If you want to check whether $path is a symbolic link whose target is /some/where, you can use the readlink utility. It isn't POSIX, but it's available on many systems (GNU/Linux, BusyBox, *BSD, …).

if [ "$(readlink -- "$path")" = /some/where ]; then …

Note that this is an exact text comparison. If the target of the link is /some//where, or if it's where 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 the test 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.

if [ "$path" -ef "/some/where" ]; then …
Share:
19,765

Related videos on Youtube

BillMan
Author by

BillMan

Updated on September 18, 2022

Comments

  • BillMan
    BillMan almost 2 years

    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.

    • A.B.
      A.B. over 9 years
    • Bratchley
      Bratchley over 9 years
      Do you mean linked to by a particular path? Couldn't you do a readlink on the known path and compare it against the path you're testing?
    • BillMan
      BillMan over 9 years
      @Bratchley. Yes, that's what I meant.. thanks for the answer.
    • BillMan
      BillMan over 9 years
      @G-Man thanks for the great feedback.. subconsciously I know to do that, but forget it in practice.. the reinforcement helps.
  • inemanja
    inemanja over 4 years
    the first example does not work when the link points to a relative path...
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 4 years
    @inemanja Sure it does. But as I state, this is a text comparison. It treats different paths to the same target as different. If you want to test whether the target of a symbolic link is a particular file, rather than a particular path, use one of the methods I describe below.
  • inemanja
    inemanja almost 4 years
    It's not safe to treat file location as strings for that exact reason - the first example compares strings hence it shows that "/dir/file.txt" and "/dir/../dir/../dir/file.txt" are different files, and they are not. The second example is much more precise.
  • Gilles 'SO- stop being evil'
    Gilles 'SO- stop being evil' over 2 years
    @MogensTrasherDK How about that? I don't understand what you're asking.
  • Mogens TrasherDK
    Mogens TrasherDK over 2 years
    If relative path is a problem, translate to absolute path. Is that more clear?
  • Mogens TrasherDK
    Mogens TrasherDK over 2 years
    BTW. I agree that the if [ "$path" -ef "/some/where" ]; then is probably a better option inmost cases.