Fresh Blurbs

Unix Shell Programming and Broken Symlinks

The well-known approach for checking whether path exists, in Unix, is: "if [ -e \$path ]" condition. However, contrary to popular belief, "-e" does not always work. The "-e" check works for folders, files etc with one exception: if there's a broken symlink on the path, it won't recognize that. Which makes sense: broken symlink is not a valid path. However, if you are writing some kind of cleanup code, you do want to be able to check on broken links, as well. To achieve that, you need to add extra condition:

if [[ -e $filepath ||  -L $filepath ]]; then
  echo "There's something at: $filepath"
comments powered by Disqus