How to check if a symlink exists

BashSymlink

Bash Problem Overview


I'm trying to check if a symlink exists in bash. Here's what I've tried.

mda=/usr/mda
if [ ! -L $mda ]; then
  echo "=> File doesn't exist"
fi


mda='/usr/mda'
if [ ! -L $mda ]; then
  echo "=> File doesn't exist"
fi

However, that doesn't work. If '!' is left out, it never triggers. And if '!' is there, it triggers every time.

Bash Solutions


Solution 1 - Bash

-L returns true if the "file" exists and is a symbolic link (the linked file may or may not exist). You want -f (returns true if file exists and is a regular file) or maybe just -e (returns true if file exists regardless of type).

According to the GNU manpage, -h is identical to -L, but according to the BSD manpage, it should not be used:

> -h file True if file exists and is a symbolic link. This operator is retained for compatibility with previous versions of this program. Do not rely on its existence; use -L instead.

Solution 2 - Bash

You can check the existence of a symlink and that it is not broken with:

[ -L ${my_link} ] && [ -e ${my_link} ]

So, the complete solution is:

if [ -L ${my_link} ] ; then
   if [ -e ${my_link} ] ; then
      echo "Good link"
   else
      echo "Broken link"
   fi
elif [ -e ${my_link} ] ; then
   echo "Not a link"
else
   echo "Missing"
fi

-L tests whether there is a symlink, broken or not. By combining with -e you can test whether the link is valid (links to a directory or file), not just whether it exists.

Solution 3 - Bash

-L is the test for file exists and is also a symbolic link

If you do not want to test for the file being a symbolic link, but just test to see if it exists regardless of type (file, directory, socket etc) then use -e

So if file is really file and not just a symbolic link you can do all these tests and get an exit status whose value indicates the error condition.

if [ ! \( -e "${file}" \) ]
then
     echo "%ERROR: file ${file} does not exist!" >&2
     exit 1
elif [ ! \( -f "${file}" \) ]
then
     echo "%ERROR: ${file} is not a file!" >&2
     exit 2
elif [ ! \( -r "${file}" \) ]
then
     echo "%ERROR: file ${file} is not readable!" >&2
     exit 3
elif [ ! \( -s "${file}" \) ]
then
     echo "%ERROR: file ${file} is empty!" >&2
     exit 4
fi

Solution 4 - Bash

Maybe this is what you are looking for. To check if a file exist and is not a link.

Try this command:

file="/usr/mda" 
[ -f $file ] && [ ! -L $file ] && echo "$file exists and is not a symlink"

Solution 5 - Bash

How about using readlink?

# if symlink, readlink returns not empty string (the symlink target)
# if string is not empty, test exits w/ 0 (normal)
#
# if non symlink, readlink returns empty string
# if string is empty, test exits w/ 1 (error)
simlink? () {
  test "$(readlink "${1}")";
}

FILE=/usr/mda

if simlink? "${FILE}"; then
  echo $FILE is a symlink
else
  echo $FILE is not a symlink
fi

Solution 6 - Bash

  1. first you can do with this style:

     mda="/usr/mda"
     if [ ! -L "${mda}" ]; then
       echo "=> File doesn't exist"
     fi
    
  2. if you want to do it in more advanced style you can write it like below:

     #!/bin/bash
     mda="$1"
     if [ -e "$1" ]; then
         if [ ! -L "$1" ]
         then
         	echo "you entry is not symlink"
         else
     	    echo "your entry is symlink"
         fi
     else
       echo "=> File doesn't exist"
     fi
    

the result of above is like:

root@linux:~# ./sym.sh /etc/passwd
you entry is not symlink
root@linux:~# ./sym.sh /usr/mda 
your entry is symlink
root@linux:~# ./sym.sh 
=> File doesn't exist

Solution 7 - Bash

Is the file really a symbolic link? If not, the usual test for existence is -r or -e.

See man test.

Solution 8 - Bash

If you are testing for file existence you want -e not -L. -L tests for a symlink.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionbearView Question on Stackoverflow
Solution 1 - BashdrysdamView Answer on Stackoverflow
Solution 2 - BashChen LevyView Answer on Stackoverflow
Solution 3 - BashCorinView Answer on Stackoverflow
Solution 4 - BashLynchView Answer on Stackoverflow
Solution 5 - BashpopedotninjaView Answer on Stackoverflow
Solution 6 - BashAmin ShateriView Answer on Stackoverflow
Solution 7 - BashDigitalRossView Answer on Stackoverflow
Solution 8 - BashAndrew LazarusView Answer on Stackoverflow