I'm playing with stat command which basically shows the inode information. Though I'm showing the information of a small file (146 characters), it shows 8 blocks. I was wondering why is that? Since the size of a page should be 4KB, which I expect the number is 1. BTW the file system I'm using is ext4. To give you more details:

more tmp.sh

#DATE=$(date +"%Y%m%d_%H%M%S")
#cp /var/log/filter.log /var/log/logHistory/filter_{$DATE}.log
dd=$(date --date='-1 day' +"%Y%m%d")
rm filter_$dd*

stat tmp.sh

  File: ‘tmp.sh’
  Size: 146         Blocks: 8          IO Block: 4096   regular file
Device: 801h/2049d  Inode: 1835522     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/timestring)   Gid: ( 1000/timestring)
Access: 2016-05-05 17:34:08.251864800 -0700
Modify: 2015-01-22 20:40:18.971521274 -0800
Change: 2015-01-22 20:40:18.975521274 -0800
 Birth: -

The "blocks" that stat() reports are 512 byte units. The normal block size used by ext4 is 4kb, or 8 of these "blocks". That means that the space used by a file on ext4 must be an integer multiple of 8 "blocks", and so the smallest size used by any file less than or equal to 4096 bytes in size is 8 512 byte blocks.

  • Are the 512 byte units related in any way to the filesystem or the underlying storage medium, or are they simply hardcoded into stat()? – Wildcard May 6 '16 at 1:33
  • 2
    @Wildcard, simply hard coded. – psusi May 6 '16 at 1:35
  • Very small files (depending on the inode size) may end up using no blocks at all; see unix.stackexchange.com/questions/197633/… for details. – Stephen Kitt May 6 '16 at 5:59

The ext4 filesystem speculates the size of a file when it is created. Quoting below from this link:

When a file is first created, the block allocator speculatively allocates 8KiB of disk space to the file on the assumption that the space will get written soon. When the file is closed, the unused speculative allocations are of course freed, but if the speculation is correct (typically the case for full writes of small files) then the file data gets written out in a single multi-block extent.

  • 1
    This answer fully misses the point; these files have already been written, and that space allocation is not at all speculative. – psusi May 6 '16 at 1:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.