How to find out the size of the file on the hard drive (preferably on perl) the stat function returns the size of the file itself ...

Something tells me that you need to normalize the size based on the current file system, namely from blockSize, but there is no certainty, can someone clarify?

OS: FreeBSD 7.2

    3 answers 3

    gaal@linux-lybs:~> du -B 1 Maple_13_InstallLog.log 4096 Maple_13_InstallLog.log gaal@linux-lybs:~> du -B 1 --apparent-size Maple_13_InstallLog.log 2487 Maple_13_InstallLog.log 

    Upper - the place occupied in FS, lower - the actual file size.

    • c du is nice, but I don’t want to call du about 10 million times :( But thanks for the answer! - Alex Kapustin
    • First, du can be called immediately for multiple files. <P> Secondly, I am sure there is a way through the pearl. - gecube

    stat :

     11 blksize preferred block size for file system I/O 12 blocks actual number of blocks allocated 

    those. It is necessary to multiply these parameters, but I have 8 times more on OpenSuse blocks than it should be, if anyone knows why, I would love to hear it (ext4 file system).

    PS and on windows they are generally empty

    • only one thing comes to mind: the value in bits, not bytes - Alex Kapustin
    • 3
      In extN, dedicated blocks are counted at 512 bytes (sector size). 8 times larger when the block size in FS 4096 (512 * 8). This is done because of backward compatibility. - eivanov
    • 3
      Even on the docks on stat () it says: "The st_blocks field indicates the number of blocks allocated to the file, 512-byte units." But in extN it is stored at the FS level. I do not know, how in other FS. - eivanov

    I would do this if necessary for a large number of files:

     #unit_size нужно определить лишь один раз для корня дерева файлов #при условии, что внутри нет примонтированных ФС $unit_size = `stat --format="%B" $some_root_dir_of_fs_or_tree`; chomp($unit_size); $ondisk_size = (stat "qwerty")[12] * $unit_size; 
    • Still need to divide by 8 - Alex Kapustin