How can I find the number of filled block of a certain file, using the stat function?

I have already found the command $(stat -c%b "$FILENAME"), which gives the number of allocated blocks but I don't know if this is the information I'm looking for?

1 Answer 1


I believe the command you have is the one that gives this required information. I did the below testing to verify if your command returns the expected output.

touch sample_file
stat -c%b sample_file 
## The output is 0 as we have no contents inside the file. 

Now, append some contents to the file.

echo "Hey there, this line goes to my file" >> sample_file
stat -c%b sample_file

Now, let us try to append more contents to see if the allocated blocks are returned correctly by the stat command. To add the contents randomly, I use the approach as discussed here.

dd bs=1024 count=1024 </dev/urandom >> sample_file
1024+0 records in
1024+0 records out
1048576 bytes (1.0 MB) copied, 0.0933755 s, 11.2 MB/s

Now, again I just check if the file contents are not overwritten just to be sure. head -1 sample_file gives me Hey there, this line goes to my file which we added earlier. Now, I run the stat command again and this is the output I get.

stat -c%b sample_file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .