file size
disk storage
measure file size
file system
data management

Get size of file on disk

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

The size of a file "on disk" is not always the same as the number of bytes in the file. Filesystems allocate space in blocks, so the physical space consumed can be larger or smaller than the logical size you see in an editor. To answer the question correctly, you need to know whether you want the file length, the allocated disk usage, or both.

Logical Size Versus Allocated Size

Logical size is the number of bytes stored in the file content. Allocated size is how much filesystem space is reserved for that file.

They differ because filesystems allocate in units such as 4096 bytes. A file with one byte of content may still occupy one full block. Sparse files can go the other direction: they may report a large logical size while consuming much less actual disk space.

So these are different questions:

  • how many bytes does the file contain
  • how many bytes of storage does the filesystem allocate

Shell Commands on Unix-Like Systems

On macOS and Linux, stat and du tell you different things:

bash
stat -c '%s bytes' myfile
du -b myfile
du -h myfile

On systems where stat -c is not available, the exact flag syntax may differ, but the concept is the same:

  • 'stat gives file metadata including logical length'
  • 'du reports disk usage'

If you care about allocated disk space, du is usually closer to the answer you want.

Using Python

Python exposes both views:

python
1from pathlib import Path
2
3path = Path("myfile")
4info = path.stat()
5
6logical_size = info.st_size
7allocated_size = info.st_blocks * 512
8
9print("logical:", logical_size)
10print("allocated:", allocated_size)

st_size is the logical byte count. st_blocks * 512 is a common way to estimate allocated size on Unix-like systems because st_blocks reports the number of 512-byte units allocated. That value is filesystem- and platform-dependent, so always treat it as operating-system metadata rather than a universal constant.

Sparse Files Change the Picture

Sparse files are an important edge case. They reserve a large logical range without allocating every block physically.

Example:

bash
truncate -s 1G sparse.bin
ls -lh sparse.bin
du -h sparse.bin

ls may show a logical size of 1G, while du shows far less actual usage. That is not an error. It is exactly what sparse allocation is designed to do.

Which Answer Should You Use

The right metric depends on your task:

  • backups and network transfer usually care about logical size
  • storage accounting usually cares about allocated size
  • quota analysis may need the filesystem's own block accounting

This is why GUI tools sometimes show both "Size" and "Size on disk." They are answering different operational questions.

Windows and Cross-Platform Notes

On Windows, APIs and command-line tools expose similar distinctions, though the field names differ. If you are writing cross-platform code, avoid assuming one Unix-specific field exists everywhere. Define clearly whether your function returns content length or consumed storage, then implement that per platform.

Ambiguity in naming is a common source of bugs. A function called get_file_size should document which definition of size it returns.

Common Pitfalls

  • Assuming file length and on-disk usage are always the same.
  • Using st_size when the real requirement is filesystem allocation.
  • Forgetting that sparse files can have huge logical sizes with small physical usage.
  • Comparing results from ls, stat, and du as if they should always match.
  • Writing cross-platform code without documenting which notion of size is returned.

Summary

  • Logical size is the byte length of the file's content.
  • Size on disk is the allocated filesystem space, often rounded to block boundaries.
  • 'stat and st_size answer the logical-size question.'
  • 'du and allocation metadata answer the on-disk-usage question.'
  • Sparse files are the clearest example of why the two numbers can differ sharply.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions