title: How does stat command calculate the blocks of a file?
body: <p>I am wondering how the <code>stat</code> command calculates the number of blocks for a file. I read this <a href="https://web.archive.org/web/20090214110539/http://linux.about.com/library/cmd/blcmdl2_stat.htm" rel="nofollow noreferrer" title="article">article</a> that says:</p>
  <blockquote>
  <p>The value st_blocks gives the size of the file in 512-byte blocks. (This may be smaller than st_size/512 e.g. when the file has holes.) The value st_blksize gives the &quot;preferred&quot; blocksize for efficient file system I/O. (Writing to a file in smaller chunks may cause an inefficient read-modify-rewrite.)</p>
  </blockquote>
  <p>Yet I cannot verify this with my own tests.</p>
  <p>My file system is ext3.</p>
  <p>The command <code>dumpe2fs -h /dev/sda3</code> shows:</p>
  <pre><code>...
  First block: 0
  Block size: 4096
  Fragment size: 4096
  ...
  </code></pre>
  <p>Then I run</p>
  <pre><code>kent@KentT60:~/Desktop$ stat Email
  File: `Email'
  Size: 965 Blocks: 8 IO Block: 4096 regular file
  Device: 80ah/2058d Inode: 746095 Links: 1
  Access: (0644/-rw-r--r--) Uid: ( 1000/ kent) Gid: ( 1000/ kent)
  Access: 2009-08-11 21:36:36.000000000 +0200
  Modify: 2009-08-11 21:36:35.000000000 +0200
  Change: 2009-08-11 21:36:35.000000000 +0200
  </code></pre>
  <p>If &quot;Blocks&quot; here means: &quot;how many 512 bytes blocks&quot;, the number should be 2, not 8. I thought that the block size of the file system (IO block) is 4k.</p>
  <p>If the file system gets the file <code>Email</code>, it will fetch a minimum of 4k from the disk (8 x 512 bytes blocks), which means 965/512 + 6 = 8. I am not sure if this guess is correct.</p>
  <p>Another test:</p>
  <pre><code>kent@KentT60:~/Desktop$ stat wxPython-demo-2.8.10.1.tar.bz2
  File: `wxPython-demo-2.8.10.1.tar.bz2'
  Size: 3605257 Blocks: 7056 IO Block: 4096 regular file
  Device: 80ah/2058d Inode: 746210 Links: 1
  Access: (0644/-rw-r--r--) Uid: ( 1000/ kent) Gid: ( 1000/ kent)
  Access: 2009-08-12 21:45:45.000000000 +0200
  Modify: 2009-08-12 21:43:46.000000000 +0200
  Change: 2009-08-12 21:43:46.000000000 +0200
  
  
  3605257/512=7041.xx = 7042
  </code></pre>
  <p>Following my guess above, this would be 7042 + 6 = 7048. But the <code>stat</code> result shows 7056.</p>
  <p>And another example from the internet at <code>https://www.computerhope.com/unix/stat.htm</code>. I pasted the example at the bottom of the page here:</p>
  <pre><code>File: `index.htm'
  Size: 17137 Blocks: 40 IO Block: 8192 regular file
  Device: 8h/8d Inode: 23161443 Links: 1
  Access: (0644/-rw-r--r--) Uid: (17433/comphope) Gid: ( 32/ www)
  Access: 2007-04-03 09:20:18.000000000 -0600
  Modify: 2007-04-01 23:13:05.000000000 -0600
  Change: 2007-04-02 16:36:21.000000000 -0600
  </code></pre>
  <p>In this example, the file system block size is 8k. I suppose the &quot;Blocks&quot; value should be 16xN, but it is 40. I'm getting lost...</p>
  <p>Can anyone explain how <code>stat</code> calculates the &quot;Blocks&quot; value?</p>
  <p>Thanks!</p>