tags:

views:

41

answers:

3

I want to delete a broken link, but before that I want to confirm if the link file is present in directory. Let's call the link A:

if [ -a A ] then 
  print 'ya A is ther'
fi

But if A is a broken link then how can I check?

A: 

Look at example 7-4 of this page: Testing for broken links.

therefromhere
+2  A: 
if readlink -qe A > /dev/null; then
    echo "link works"
fi
John Kugelman
Note that this assumes (recent-ish) GNU coreutils. There are unix systems without a `readlink` command, or with a `readlink` command that takes different options.
Gilles
A: 

find -L -type l finds broken symbolic links. First confirm that the file is a symbolic link with test -L (if it's a directory, find would recurse into it). Thus:

is_broken_symlink () {
    case $1 in -*) set "./$1";; esac
    [ -L "$1" ] && [ -n "$(find -L "$1" -type l)" ]
}
Gilles