#!/usr/bin/perl use strict; use IO::File; my $size; my $inode; my $sum = 0; my @entries; my $e; my $percent = 0; my $remsum = 0; my $counter = 0; my $nofiles = 0; my $du = new IO::File; if (@ARGV) { chdir "$ARGV[0]" or die "cannot change to [ $ARGV[0] ]\n"; } my $path = $ARGV[0] . "/"; print "$path\n"; START: $du->open("du -sk *|") or die "cannot open du program and pipe"; while (<$du>) { ($size, $inode)=split; $inode .= "/" if (-d $inode); $sum += $size; push @entries, { size => $size, inode => $inode }; } @entries = sort { $b->{size} <=> $a->{size} } @entries; $du->close; foreach $e (@entries) { $percent = $e->{size}/$sum*100; if ($counter < 10) { printf( "%30s | %5d | %5.2f%%\n", $e->{inode}, $e->{size}, $percent); } else { $nofiles++; $remsum += $e->{size}; } $counter++; } if ($remsum > 0) { printf( "%30s | %5d | %5.2f%%\n", "files", $remsum, $remsum/$sum*100); } if (@entries[0]->{inode} =~ /(.*\/$)/) { $path .= $1; chdir $path; print "$path\n"; undef @entries; $counter=0; $nofiles=0; goto START; } else { print "No more!"; exit; }
Tuesday, 26 May 2015
Disk Usage - What's eating my disk space Perl script
Subscribe to:
Posts (Atom)