From c90378dce35ee61e2ec29632ebc92a0631891033 Mon Sep 17 00:00:00 2001 From: Fred Boniface Date: Mon, 2 Oct 2023 23:05:58 +0100 Subject: [PATCH] ds can now handle relative and absolute paths --- interpreted/ds | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/interpreted/ds b/interpreted/ds index 3e5f58a..ba1ff40 100755 --- a/interpreted/ds +++ b/interpreted/ds @@ -24,18 +24,39 @@ use strict; use warnings; use File::Find; +use Cwd; +use Getopt::Long; -if (@ARGV == 0) { +my $help; +my $verbose; + +my $usage = "Usage: $0 \n"; + +GetOptions( + 'help|h' => \$help, + 'verbose|v' => \$verbose, +) or die $usage; + +if ($help) { print < - +\n$usage 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 exit; } +if (@ARGV != 1) { + die $usage; +} + my $dir_path = $ARGV[0]; +$dir_path = Cwd::abs_path($dir_path); + unless (-d $dir_path) { die "$dir_path is not a directory\n" } @@ -53,6 +74,15 @@ sub calculate_size { if (-f $file) { $total_size += -s $file; + if ($verbose) { + print "File: $file\n" + } + } + + if (-d $file) { + if ($verbose) { + print "Directory: $file\n" + } } }