Check for symbolic link
Asked Answered
S

2

17

I have a symbolic link a.c in my home directory to another file in the same directory.

a.c -> b.c

I know how to check a.c is a symbolic link using the shell script

if [ -L /home/nit/a.c ] ; then
    echo "a.c is a symbolic link"
fi

But my question is how to write a shell script to check whether a.c is a symbolic link specifically to b.c ?

Thanks

Static answered 30/12, 2010 at 8:37 Comment(0)
M
28

Use readlink;

[~]> ln -s foo bar
[~]> readlink bar 
foo
Multinational answered 30/12, 2010 at 8:40 Comment(1)
So, if [[ -L a.c ]] && [[ "$(readlink a.c)" = "b.c" ]]; then echo "a.c is a link to b.c"; fiMilled
S
6

You could also do ls -F filename.txt which returns filename.txt@ if it is a symbolic link.

Skit answered 12/2, 2011 at 21:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.