Here's a piece of kit from my personal toolbox. I'm a Unix sysadmin and I sometimes run into directories with millions of files - such as mail spools choking with spam. The system tools will regularly choke on these. I've used these tools with great success. At one site I used these to clean up spam-infested mail queues when the built-in tools of a major commercial MTA weren't fast enough.
Sometimes the only thing of interest is the file count:
Sometimes you need to search for files with some criteria and do something to them. Here's a script that searches for files based on name given a directory and regexp:use strict; use warnings; my $dir = shift; die "Usage: $0 directory" unless defined $dir; opendir DIR, "$dir" or die "Could not open $dir: $!\n"; my @files=readdir DIR; print $#files+1, "\n"; closedir DIR;
The logic here is that this only picks the relevant files from the directory array, which is a reasonably fast operation even with a million-row array, and only touches the files in the result set. So if you have a directory with a million files, and there's one file you want to know about, you don't need to run through the other files going "no, that's not it..."use strict; use warnings; my $dir = shift; my $criteria = shift; $criteria = "" unless defined $criteria; die "Usage: $0 directory" unless defined $dir; opendir DIR, "$dir" or die "Could not open $dir: $!\n"; my @files=grep(/$criteria/, readdir DIR); print $#files+1, " files\n"; chdir $dir; foreach my $file (@files) { # actions go here } closedir DIR;
You'll notice a certain toollike air about these scripts, as if they were covered in grease, scratched and dinged from being thrown about in a toolbox. This is exactly the case. These get copied or rewritten every so often into random systems that don't necessarily have any CPAN modules or any practical way to install them in any reasonable amount of time. That's why the Spartan interface and simple structure. I've written a fancier version with options, lots of features, and nicely formatted output, but that's apparently left that behind on a previous employer's server. Dang.
|
|---|