All pastes #738210 Raw Edit

rgrep.pl

public text v1 · immutable
#738210 ·published 2007-10-16 02:43 UTC
rendered paste body
#!/usr/bin/perl -w

# Like grep -r except...
#   * you can leave off the directory and it will use . instead of waiting like
#     a dumbshit for STDIN
#   * It handles paths with spaces and quotes.
#   * it will not traverse into these directories or files.
my @Prunes = (qw(.svn CVS blib *~ *.bak _darcs tags), '#*');

my @Dirs;
my @Args;

my $saw_pattern = 0;
# grep --opt1 --opt2 pattern dir1 dir2 dir3
for (@ARGV) {
    if( /^-/ ) {
        push @Args, $_;
    }
    elsif( $saw_pattern ) {
        push @Dirs, @_;
    }
    else {
        push @Args, $_;
        $saw_pattern = 1;
    }
}
@Dirs = ('.') unless @Dirs;

@Args = map { "'$_'" } 
        map { s/'/'"'"'/g; $_ } @Args;

# Escape spaces and quotes
@Dirs = map { s{([ '"])}{\\$1}g; $_ } @Dirs;

my $prunes = join ' -o ', map { "-name '$_' -prune" } @Prunes;
exec "find @Dirs $prunes -o -type f -print0 | ".
     "xargs -0 grep @Args";