Description: Ever get that pesky error 'Arg list too long' when you're trying to rm a bunch of files? Yeah, it's a limitation of the shell, not rm. But, it's an annoyance nonetheless. So, this morning I whipped this little baby up in about an hour.

Feedback is VERY much appreciated!

#!/usr/bin/perl -w

use strict;
use Getopt::Long;
use File::Basename;

## Written by Jim Conner 24 Jan 2006
## <snafuxnj ->at<- yeah-hoo (dot) com>
## Purpose: When the command `rm *` fails due
## to shell limitations for glob operations
## aka * wildcard equals too many arguments for
## substitution to work properly then you can
## use this little script to remove everything
## in the directory specified.  It DOES NOT
## work recursively.  At least not yet.

my @DIRS;

my $rev        = '0.1';
my $plProgName = basename($0);

GetOptions('directory=s' => \@DIRS           ,
           'help'        => \&HELP_MESSAGE   ,
           'version'     => \&VERSION_MESSAGE,

if ( scalar(@DIRS) > 0 )
    for my $dir ( @DIRS )
        if ( -d $dir )
            if ( chdir($dir) )
                unless ( my $num_unlinked = unlink(<*>) )
                    print STDERR 'Unable to remove all files in '.$dir
                    print STDERR ': '.$!,"\n";
                    print $num_unlinked .' files were removed.',"\n";
                print STDERR 'Unable to chdir to '.$dir.': '.$!,"\n";
            print STDERR $dir,' is not a valid directory or is unreada


    print <<EOM

    Usage: $plProgName <--directory|-d <directory>> --directory|-d <di

    You must specify -d argument.

    NOTE:  Once you successfully run this program for a directory
           the files within that directory will ALL be removed.
           This program DOES NOT recursively remove files and it
           will NOT remove directories.


    print "\n".$plProgName,"\n".
          'Version: '.$rev,"\n\n";


=head1 NAME

bigrm - Remove all files non-recursively in a given set of directories


Ever get the error from the rm command: 'Arg list too long'?  This is 
problem with the shell and not rm.  It happens when you supply the '*'
glob wildcard to the command rm.  So, I wrote this little program to
help with this little annoyance.  This nifty little guy will simply re
every non-directory file in a directory(ies) specified on the command 
It will NOT remove directories in the directory(ies) nor will it recur
work in the directory(ies) specified.

That functionality can be written into this program but its not what I
needed and I don't have time to write it in.  I whipped this little gu
out in about 1 hour (had to read up on some Getopts::Long stuff).

=head1 USAGE

bigrm <--directory|-d <directory>> --directory|-d <directory>...

=head1  SEE ALSO

rm(1), unlink(1)

=head1 AUTHOR

Jim Conner <snafuxnj ->at<- yeah-hoo (dot) com>

Replies are listed 'Best First'.
Re: bigrm: remove everything in the directory specified
by duff (Parson) on Jan 24, 2006 at 21:35 UTC

    When I get an "Arg list too long" error, I reach for xargs(1) since it's the canonical method for solving this problem.