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";
exit(1);
}
else
{
print $num_unlinked .' files were removed.',"\n";
}
}
else
{
print STDERR 'Unable to chdir to '.$dir.': '.$!,"\n";
exit(2);
}
}
else
{
print STDERR $dir,' is not a valid directory or is unreada
+ble.',"\n";
exit(4);
}
}
}
else
{
HELP_MESSAGE();
}
exit(0);
sub HELP_MESSAGE
{
print <<EOM
Usage: $plProgName <--directory|-d <directory>> --directory|-d <di
+rectory>...
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.
EOM
;
exit;
}
sub VERSION_MESSAGE
{
print "\n".$plProgName,"\n".
'Version: '.$rev,"\n\n";
}
#vim:ts=8:sts=4:sw=4:nu:ai:sta:si:sm
=pod
=head1 NAME
bigrm - Remove all files non-recursively in a given set of directories
+.
=head1 DESCRIPTION
Ever get the error from the rm command: 'Arg list too long'? This is
+a
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
+move
every non-directory file in a directory(ies) specified on the command
+line.
It will NOT remove directories in the directory(ies) nor will it recur
+sively
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
+y
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>
=cut