#!/usr/bin/perl use strict; use warnings; use File::Find qw( find ); my ($path) = @ARGV or die("usage: perl dir_size.pl dirpath\n"); opendir(my $dh, $path) or die("Can't open dir $path: $!\n"); while (defined(my $dir = readdir($dh)) { next if $dir =~ /^\./; my $full_dir = "$path/$subdir"; next if ! -d $full_dir; my $total = 0; find(sub { $total += -s if -f $_ }, $full_dir); print "The total size of the files in $dir is " . sprintf("%.2f Kb", ($total/1024)) . "\n"; print "The total size of the files in $dir is " . sprintf("%.2f Mb", ($total/(1024*1024))) . "\n"; }
I don't know why people use a named sub for find. If I did want to split out the logic into a separate sub, I'd do something like
sub some_sub { my @results; find(sub { push @results, $_ if check($_) }); ... } sub check { ... Check if the file matches. Doesn't know about File::Find ... }
In reply to Re^2: Dir size
by ikegami
in thread Dir size
by larus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |