Re: [SLUG] links

From: Dylan William Hardison (dylan@hardison.net)
Date: Sat May 17 2008 - 16:36:36 EDT


Spake James Forte on Saturday, May 17, 2008 at 08:58AM -0400:
>
> Build a simple shell script. That's what linux is all about.
>
> iterate over each file that 'find -type l' finds and check it with any
> command that opens a file... like the 'file' command.
>
> Every broken link will have a different error message.
>
> for F in `find /james/username -type -l`
> do
> RES=`file $F`
> if [ echo $RES | grep broken ]
> then
> echo $RES
> fi
> done

Using "for" is generally a bad idea, in that it will usually not work when
given filenames with spaces in them.
Better to use find with -exec and a simple shell script:

#!/bin/bash
# filename: checklink
# suggested usage: find -type l -exec ./checklink {} \; | xargs rm -vf

file="$1"

if [[ -z $file ]]; then
        echo "usage: checklink path"
        exit 1
fi

target=$(readlink "$file")
cd $(dirname "$file")

while next_target=$(readlink "$target"); do
        cd $(dirname "$next_target")
        target="$next_target"
done

[[ ! -e $target ]] && echo $file

-- 
Our OS who art in CPU, UNIX be thy name. Thy programs run, thy syscalls
done, In kernel as it is in user!
-
GPG Fingerprint: 412C CCE9 DDA2 4FE9 C34F  754B 0863 0EA6 712E BBE1
-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS).  Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 15:51:37 EDT