How much space in a directory

Sorry for the lack of points, but this is all I've got.

How do I find out how much space a dir is taking up?
I need to know how in Perl for Unix and NT.
Who is Participating?
jhurstConnect With a Mentor Commented:
assuming that you mean the files in the directoy, not the directory itself, ls -l will give you the size of each file, then use perl to read this output and add the appropriate column
> ls -l will give you the size of each file

This will work only on Unix, where ls command is available.

If you are on unix, it can be even simpler. The du command will give you what you want. If you use the -s option, it will give you the space occupied by a directory and all its sub-directories. The value you get is the number of blocks which vary depending on the system. If you want the value in number of kilobytes, use the -k option too.

$space_in_KB = `du -sk $directory`;

On NT there might be a module for this kind of info, but I am not sure. In the absence of a module, you will need to open the directory, read all file names and get the size of each file and add them up.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.