Write a Perl program that counts the number of files in the working direc- tory and the number of bytes in those files, by filename extension.
What will be an ideal response?
$ cat filetype-summary.pl
#!/usr/bin/perl
use warnings;
use strict;
# Hashes to accumulate file counts and bytes used
my %count;
my %bytes;
for my $filename ( glob( "*" ) ) {
if ( -f $filename ) {
my $type;
# Find the type of file
if ( $filename =~ /.+\.([^.]+)$/ ) {
$type = '.' . $1;
}
else {
$type = '
}
# Increment the count for that type
++$count{ $type };
# Use the -s file test operator to get the bytes used
# and add it to the total
$bytes{ $type } += -s $filename;
}
}
# Take the list of types and sort it, and for each
# type print the count and bytes for each
for my $type ( sort keys %count ) {
printf( "%4d files, %8d bytes: %s\n", $count{$type}, $bytes{$type}, $type );
}
You might also like to view...
Notes pages are printouts that display the speaker notes in the ________ of the page
A) lower half B) right side C) upper half D) left side
Dirty data is ___
A. virus infected B. worm infected C. inaccurate, incomplete data D. stolen data