Directory Size

shaunm
shaunm used Ask the Experts™
on
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
Comment
Watch Question

Do more with

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

Commented:
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
Commented:
A more Perl way (particularly for Windows) would be:

$path='c:/windows/system/foo/bar/baz';
$sum=0;
opendir(D, $path) || die;
foreach(readdir D) {
     if (-f "$path/$_") {
          $sum+=(-s _);
     }
}
closedir(D);

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

Author

Commented:
Thanks

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