#!/usr/bin/perl use strict; use warnings; use Cwd; my $test=0; my $name; my $wert; my $dir; use Getopt::Std; my %opts; getopts('hd:f:m', \%opts); if ($opts{h}) { print "Usage:\n"; print "filesize.pl -h for help\n"; print "filesize.pl -f [filename] to show the size of [filename]\n"; print "filesize.pl -d [directory] to show the sizes of all files in [directory]\n"; print "filesize.pl to show the sizes of all files in the current directory\n"; } elsif ($opts{f}) { my $f = getcwd; opendir(DIR, "$f"); my @dir = readdir(DIR); closedir(DIR); foreach (@dir) { if ($_ eq $opts{f}) { $test=1; } } if ($test) { $_ = (-s "$opts{f}"); $wert = &size($_);; $name = $opts{f}; write; } else { print "Datei $opts{f} exisiert nicht!\n"; } } $dir = getcwd unless(defined($opts{d})); if (defined($opts{d})) { if (-d "$opts{d}") { $dir = $opts{d}; } else { print "$opts{d} existiert nicht oder ist kein Verzeichniss!\n"; exit 0; } } unless (defined($opts{h}) || defined($opts{f})) { # opendir(DIR, "$dir") or die $!; opendir(DIR, ".") or die $!; my @array = readdir(DIR); closedir(DIR); my %hash; my %for; my @size; format STDOUT_TOP = Dateiname Dateigroesse ==================================== . foreach(@array) { my $i = 0; if ($opts{d}) { chdir $dir; } $hash{$_} = (-s $_); my $file = (-s $_); $for{$file} = $_; $i++; push @size, $file; } my @sort = sort { $a <=> $b } @size; foreach(@sort) { if (-f "$for{$_}") { my $data = $_; &size($_); $name = $for{$data}; $wert = $_; format STDOUT = @<<<<<<<<<<<<<<<<< @>>>>>>>>>>>>>>>> $name, $wert . write; } } } sub size { if ($_ < 1024) { $_ .= " bytes"; } elsif ( $_ > 1024 && $_ < 1048576) { $_ = sprintf("%.2f", $_ / 1024); $_ .= " kb"; } elsif ( $_ > 1048576 && $_ < 1099511627776) { $_ = sprintf("%.2f", $_ / 1048576); $_ .= " mb"; } else { $_ = sprintf("%.2f", $_ / 1099511627776); $_ .= " gb"; } return $_; }