1: #!/bin/sh
   2: # For people who eat megabytes like corn chips.. 
   3: #
   4: # Inspired by overflowing partitions and the pretty shell
   5: # code in Tales from the Abyss: UNIX File Recovery (SysAdmin
   6: # Mag) at http://www.samag.com/documents/s=1441/sam0111b/0111b.htm
   7: # You can also do things in shell like:
   8: #  for i in `find . `; do file $i | grep ASCII ; done 
   9: # Or replace $1 with a directory and paste from "find" at prompt.
  10: # No need to shy away from the command line! read man bash
  11: # (or man tcsh) and get results fast.
  12: #
  13: echo "Recursive search for symbolic links.  Usage: findlinks dirname";
  14: find $1 | file -f - | grep link\ to | awk -F: '{print $2 "\t" $1}' | sort -b +1 | perl -e '
  15: $sy = "symbolic link"; $sb = "broken symbol"; $sl = length($sy); 
  16: while (<>) { 
  17:  $m=$_; $m =~ s/^\s+//; 
  18:  $n = $m;
  19:  $n =~ s/^(.+)\t(.+)$/$2\t\($1\)/; #WAS $n =~ s/^(.+)\t(.+)$/$2/;
  20:  if    (substr($m,0,$sl) eq $sy) {push @sys,$n;} 
  21:  elsif (substr($m,0,$sl) eq $sb) {push @sbs,$n;} 
  22:  else                            {push @oth,$n;} #for debug
  23: }
  24: print "\n** Symbolic Links:\n", @sys, "\n** Broken Symbolic Links:\n", @sbs;
  25: print "\nOthers:\n", @oth unless $#oth<0;
  26: '

Replies are listed 'Best First'.
Re: Unix Administration: Finding symbolic links with perl and *nix
by frag (Hermit) on Nov 14, 2001 at 06:26 UTC
    Well, TMTOWTDI, but I can't say that this will keep me from shying away from /.{0,3}sh/ scripts! Here's one Perl way to do it, courtesy of the Cookbook:
    #!/usr/bin/perl -w use File::Find; find(\&wanted, shift || '.'); sub wanted { -l and not -e and print "bogus link: $File::Find::name\n"; }

    -- Frag.
    --
    "Just remember what ol' Jack Burton does when the earth quakes, the poison arrows fall from the sky, and the pillars of Heaven shake. Yeah, Jack Burton just looks that big old storm right in the eye and says, "Give me your best shot. I can take it."

      Yep, you're right. Perl *does* rule.

      My motivation really came about when wrestling with a mixture of different ticks from a shell for loop and an awk command in that article.. this didn't work:

      # for i in 'file * | grep "ASCII C program text" | \ awk -F: '{print $1}'`; do mv $i $i.c; done
      The different ticks are tricky. You can try it by erasing "C program text" and changing mv to echo. Hmm, still not sure if that should work or not.

      Anyway, now instead of always relying on perl -pi (which also rules, let it be said) I feel personally a little more comfortable with piped operations on lists of files on the command line. Though I find that some shell tools pad output to both left and right justify at the same time.. this is a bad thing and Perl is still what I use for gardening.