You’ve seen the problem – many Unix commands don’t like very long argument lists.  For instance, my Linux machine complains when I try to “rm” 10,000 files at once:

[eddie@yoakam test]$ rm *
-bash: /bin/rm: Argument list too long

Here’s a simple script that will delete all of the files in a given directory, even if the argument list gets really long. I prefer to keep it in my ~/bin directory named “bigrm”.

My bigrm Perl script:

#!/usr/bin/perl

if (!$ARGV[0]){
   print "Usage:  $0 \n";
   print "Will delete all files in specified directory\n";
   exit(1);
}

print "deleting files from $d\n";
$OUTDIR=$ARGV[0];

opendir(DIR, $OUTDIR) || die ("cant opendir $IMAGE_DIR: $!");
print "OPENED $OUTDIR\n";
while ($file = readdir(DIR)){
   if ($file!~/^\./){
      print "unlink $file\n";
      unlink ("$OUTDIR/$file");
   }
}
closedir(DIR)

This is useful for cleaning out directories of temp files, of snapshot directories, etc.