Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

bigrm: remove everything in the directory specified

by snafu (Chaplain)
on Jan 24, 2006 at 18:55 UTC ( #525287=snippet: print w/replies, xml ) Need Help??
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";
                    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

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://525287]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2019-05-26 19:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you enjoy 3D movies?



    Results (153 votes). Check out past polls.

    Notices?
    • (Sep 10, 2018 at 22:53 UTC) Welcome new users!