How I can know if file1 is a symbolic link to a file2?
I need an if condition.
Asked
Active
Viewed 295 times
2
Stephen Kitt
- 411,918
- 54
- 1,065
- 1,164
Edward
- 19
- 3
1 Answers
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
- 320,670
- 36
- 633
- 936
Stephen Kitt
- 411,918
- 54
- 1,065
- 1,164
-
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