#!/usr/bin/perl use strict; use warnings; use Cwd; use Getopt::Std; use vars qw($opt_f $opt_h $opt_d); my $test=0; my $name; my $wert; my $dir; if (! getopts('f:hd:')) { } if ($opt_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 ($opt_f) { my $f = getcwd; opendir(DIR, "$f"); my @dir = readdir(DIR); closedir(DIR); foreach (@dir) { if ($_ eq $opt_f) { $test=1; } } if ($test) { $_ = (-s "$opt_f"); $wert = &size($_);; $name = $opt_f; write; } else { print "Datei $opt_f exisiert nicht!\n"; } } $dir = getcwd unless(defined($opt_d)); if (defined($opt_d)) { if (-d "$opt_d") { $dir = $opt_d; } else { print "$opt_d existiert nicht oder ist kein Verzeichniss!\n"; exit 0; } } unless (defined($opt_h) || defined($opt_f)) { opendir(DIR, "$dir"); my @array = readdir(DIR); closedir(DIR); my %hash; my %for; my @size; format STDOUT_TOP = Dateiname Dateigroesse ==================================== . foreach(@array) { my $i = 0; $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 $_; }