Re: Total size of each Directory
by broquaint (Abbot) on Jul 28, 2003 at 11:48 UTC
|
use List::Util 'sum';
use File::Find::Rule;
my $dirsize = sum map -s, find( file => in => shift || '.' );
HTH
_________ broquaint | [reply] [d/l] |
Re: Total size of each Directory
by bart (Canon) on Jul 28, 2003 at 13:04 UTC
|
Whatever you do, be careful about interpreting the results. In general, a file takes more space on disk than the file size itself would indicate. Files are stored on disk in clusters, which is some whole multiple of 512 or 1024 bytes (= one sector). The size of a cluster is related to the disk size, in general, the larger the disk, the larger the cluster size. A value of 16 to 32k is quite common. Thus, even if your file is only 1 byte long, it will take up a whole cluster. In a similar way, if your file is just 1 byte too big to fit into one cluster, it will take up two.
In pop speak: you should round up the file size for each to the next multiple of the cluster size, and only add up those results. Only that result will actually tell you how much space is taken up by a directory. | [reply] |
|
|
...Only that result will actually tell you how much space is taken up by a directory...
On the other hand, if you're interested in finding out how much space would be used by the directory e.g. when written to a tar-file, then simply adding the numbers will give you a rough idea of the final size of the tar-file.
And of course, if you're using a filesystem like ReiserFS, who knows how much space you are actually using in a directory...
Liz
| [reply] |
|
|
As other monks have mentioned, there is always the venerable unix du utility. This tells how much actual disk space is being used by the files, as opposed to the combined size of the files. Pretty handy. (Though, also noted by another monk, sometimes it's useful to simply know the total combined size of some files. e.g. for burning to a cd or creating a tar.)
| [reply] [d/l] |
|
|
Both CD-ROMs and tar files use sector blocks.
For tar files, the block size is 512 bytes.
The block size on a CD is a somewhat over 2300 bytes. For data, the surplus is used for error detection and correction, so the block size for actual data is 2048 bytes. For details, take a look here or here.
Thus, in summary, for both CD-ROM and for tar files, to get the actual size on disk disk, you need to apply the same calculations, but with different sector sizes than those that apply to the harddisk.
| [reply] |
|
|
Re: Total size of each Directory
by BazB (Priest) on Jul 28, 2003 at 11:45 UTC
|
| [reply] [d/l] |
|
|
du has also been rewritten in perl at the PPT site.
~~
naChoZ
| [reply] |
Re: Total size of each Directory
by davorg (Chancellor) on Jul 28, 2003 at 11:44 UTC
|
You'll probably find the -S file test operator to be very useful.
--
<http://www.dave.org.uk>
"The first rule of Perl club is you do not talk about
Perl club." -- Chip Salzenberg
| [reply] |
Re: Total size of each Directory
by Lachesis (Friar) on Jul 28, 2003 at 11:53 UTC
|
my first thought would be to just run du on your command line.
If you want to do it in perl the -s filetest will give you the size of a file | [reply] |
Re: Total size of each Directory
by rir (Vicar) on Jul 28, 2003 at 16:04 UTC
|
Along the lines of what bart and liz said you
also want to decide how you count symlinks. | [reply] |
|
|
Don't forget hard links either. These can skew the results dramatically... Two "separate" files that are literally pointing to the same block of disk, but are counted twice, can be a major problem. I know this from experience. :)
| [reply] |