Re: [SLUG] links

From: Eben King (eben01@verizon.net)
Date: Sat May 17 2008 - 19:05:21 EDT


On Sat, 17 May 2008, Dylan William Hardison wrote:

> 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

"find ... -exec" runs the script once for each found item. With a script
that can handle multiple args, this is possible:

find ... -print0 | xargs -0 script
          ^^^^^^^ ^^
filenames separated with \0

Runs the script once for each 20K(?) (or fraction thereof) of results.

I got readlink installed (either the dist site was down or my ISP's routing
was messed up). Now checking the man page to see how it's usable.

-- 
-eben   QebWenE01R@vTerYizUonI.nOetP   http://royalty.mine.nu:81

Hanlon's Razor: "Never attribute to malice that which can be adequately explained by stupidity." Derived from Robert Heinlein ----------------------------------------------------------------------- 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:52:00 EDT