Wednesday, September 29, 2010

A perl implementation of find

Recently working on a system which has a busted find command need a fix.. it is about 300% slower than the real thing so use it when needed only

#!/usr/bin/env perl

use strict;
use warnings;
use Cwd;

my $start=$ARGV[0];

sub find
{
    my ($dirname) = @_;
    my $dh;
    opendir($dh, $dirname) or die "Couldn't open dir '$dirname': $!";
    my @files = readdir($dh);
    closedir($dh);

    #print files...
    foreach my $file (@files) 
    {
 if(
    ($file) &&
    ($file !~ /^\.$/) &&
    ($file !~ /^\.\.$/)
    )
 {
     my $path = "$dirname/$file";
     print "$path\n";
     if(-d "$path")
     { 
  find("$path"); 
     }
 }
    }
}

$start = "." unless $start;

find($start);

No comments:

Post a Comment