I am scanning a file system and wish to determine the allocated size of a file on disk.
Using stat() I can get the file size as ( stat(_) )[7] , the blocks allocated on disk can be obtained from ( stat(_) )[12], and the “preferred block size for file system I/O” is ( stat(_) )[11].
If I merely multiply stat column 11 and 12, however, I do not get what appears to be the allocated space on disk (on Solaris 5.10 sparc).
How can I programmatically get the space allocated on disk for a file from the stat() function call in Perl?
The value exposed in
(stat _)[11]isst_blksize, which is documented asThis is not necessarily the block size of the particular filesystem on which your file resides, but the same manual page contains a convenient definition:
So you could use code such as
If you’re concerned that the block sizes of your filesystems aren’t multiples of 512, double-check with either
or if you have root
For an ordinary file, the size of the file itself, i.e.,
(stat _)[7], is usually smaller than the total size of all blocks allocated because filesystems allocate whole blocks.