ds can now handle relative and absolute paths

This commit is contained in:
Fred Boniface 2023-10-02 23:05:58 +01:00
parent 88b388993d
commit c90378dce3
1 changed files with 33 additions and 3 deletions

View File

@ -24,18 +24,39 @@
use strict; use strict;
use warnings; use warnings;
use File::Find; use File::Find;
use Cwd;
use Getopt::Long;
if (@ARGV == 0) { my $help;
my $verbose;
my $usage = "Usage: $0 <directory_path>\n";
GetOptions(
'help|h' => \$help,
'verbose|v' => \$verbose,
) or die $usage;
if ($help) {
print <<EOF; print <<EOF;
\nUsage: $0 <directory_path> \n$usage
Calculate the size of a directory and its contents.\n Calculate the size of a directory and its contents.\n
Options:
-h, --help Show this help message and exit
-v, --verbose List all files and folders encountered
EOF EOF
exit; exit;
} }
if (@ARGV != 1) {
die $usage;
}
my $dir_path = $ARGV[0]; my $dir_path = $ARGV[0];
$dir_path = Cwd::abs_path($dir_path);
unless (-d $dir_path) { unless (-d $dir_path) {
die "$dir_path is not a directory\n" die "$dir_path is not a directory\n"
} }
@ -53,6 +74,15 @@ sub calculate_size {
if (-f $file) { if (-f $file) {
$total_size += -s $file; $total_size += -s $file;
if ($verbose) {
print "File: $file\n"
}
}
if (-d $file) {
if ($verbose) {
print "Directory: $file\n"
}
} }
} }