user3161924
user3161924

Reputation: 2273

bash: check if file or symlink (even dangling) exists?

I need to check if a file exists in bash. The -e is not working for a dangling symlink.

In this case I copied ld-linux.so.2 to another directory. The relative link points to i386-linux-gnu/ld-2.28.so which doesn't exist.

When I run this (from the directory ld-linux.so.2 is in):

if [[ -e ld-linux.so.2 ]]; then  echo "yes"; fi

The result is nothing prints which suggests that -e is checking the target and not the symlink itself. How can I just check if the file exists, not the file it points to?

TIA!!

Upvotes: 0

Views: 398

Answers (1)

M. Nejat Aydin
M. Nejat Aydin

Reputation: 10113

Use -h to test if a symlink exists:

if [[ -h ld-linux.so.2 || -e ld-linux.so.2 ]]; then  echo "yes"; fi

Upvotes: 2

Related Questions