Looking for reaper skeleton

Larry Wall lwall at jpl-devvax.JPL.NASA.GOV
Tue Sep 11 11:33:37 AEST 1990


In article <1990Sep10.230742.9600 at indetech.com> david at indetech.com (David Kuder) writes:
: Or: Does anyone have a skeleton for a find equivalent written in Perl?
: I think I could do what I want from that.

This one will beat find in elapsed time on my machine, as long as you're
primarily examining just the names.  If you have to stat every file, it'll
run a bit slower, but you can do away with the $nlink == 2 business.

#!/usr/local/bin/perl

&dodir('.');
sub dodir {
    local($dir,$nlink) = @_;
    local($dev,$ino,$mode);
    ($dev,$ino,$mode,$nlink) = stat('.') unless $nlink;	# first time

    opendir(DIR,'.') || die "Can't open $dir";
    local(@filenames) = readdir(DIR);
    closedir(DIR);

    if ($nlink == 2) {			# this dir has no subdirectories
	for (@filenames) {
	    next if $_ eq '.';
	    next if $_ eq '..';
	    print "$dir/$_\n";
	}
    }
    else {				# this dir has subdirectories
	for (@filenames) {
	    next if $_ eq '.';
	    next if $_ eq '..';
	    $name = "$dir/$_";
	    print $name,"\n";

	    ($dev,$ino,$mode,$nlink) = lstat($_);
	    next unless -d _;

	    chdir $_ || die "Can't cd to $name";
	    &dodir($name,$nlink);
	    chdir '..';
	}
    }
}

Larry Wall
lwall at jpl-devvax.jpl.nasa.gov



More information about the Comp.unix.admin mailing list