Symlink check - Linux Bash Script Symlink check - Linux Bash Script shell shell

Symlink check - Linux Bash Script


Check this page. It has a test for broken links. It uses the -h operator to identify a symlink and the -e operator to check existance.

From that page:

linkchk () {    for element in $1/*; do      [ -h "$element" -a ! -e "$element" ] && echo \"$element\"      [ -d "$element" ] && linkchk $element    # Of course, '-h' tests for symbolic link, '-d' for directory.    done}#  Send each arg that was passed to the script to the linkchk() function#+ if it is a valid directoy.  If not, then print the error message#+ and usage info.##################for directory in $directorys; do    if [ -d $directory ]    then linkchk $directory    else         echo "$directory is not a directory"        echo "Usage: $0 dir1 dir2 ..."    fidoneexit $?


You can test whether link is valid or not using:

[[ -f "$link" ]] && echo "points to a valid file"

To check if it is indeed a link use -L:

[[ -L "$link" ]] && echo "it's a link"


There seems to be a program named symlinks that does, among other things, what you're looking for.