2

How I can know if file1 is a symbolic link to a file2? I need an if condition.

Stephen Kitt
  • 434,908
Edward
  • 19

1 Answers1

3

You can use the -h test to determine whether a file is a symbolic link, and -ef to check if it links to a given file (note that -ef isn’t specified by POSIX):

if [ -h file1 ] && [ file1 -ef file2 ]; then
    echo 'file1 is a symbolic link and equivalent to file2'
fi
Kusalananda
  • 333,661
Stephen Kitt
  • 434,908
  • I suppose both files could be two different names for the same symbolic link too, pointing to some third pathname, but that might be a too weird edge case. – Kusalananda Jun 05 '22 at 09:26