Directory Size

shaunm used Ask the Experts™
I would like to determine the size of a directory and all the files and the directories below them.

Simple script but Ihave no idea where to begin as I am new to perl.

I have done something similar in php using

system("du /directory/")

but would prefer integrating it with my crrentcgi script

i will hard code the starting directory path once I know how

Thanks in advance
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Actually you shouldnt need the full path.

You would do something like:
$size = `du -sh ../../directory`;
(note: ../ moves you to the parent directory, so you want need to know the full directory path)

The whole subdirectory structure, and files will need to be readable by nobody (they should be anyway, otherwise apache cant read them).
Top Expert 2007
A more Perl way (particularly for Windows) would be:

opendir(D, $path) || die;
foreach(readdir D) {
     if (-f "$path/$_") {
          $sum+=(-s _);

print "Total bytes for $path: $sum\n";



Exactly what I needed!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial