How to Know if a File on Linux is Sparse?

Patrick Jayet
Mar 30, 2010 · 1 min read

A sparse file is a file which does not take more space on disk than needed. Such a file is usually used to store a partition image on disk, for instance with a virtualization solution like Xen.

It’s super easy, to know if a file is sparse or not. Just use the ‘s’ option of ls.

ls -alsh

will yield:

4.0K drwxr-xr-x 9 root root 4.0K 2010-03-23 18:22 .
4.0K drwxr-xr-x 23 root root 4.0K 2009-01-09 19:47 ..
12G -rw-r----- 1 root root 24G 2007-01-10 19:55 dompat.data
3.7G -rw-r--r-- 1 root root 7.1G 2009-01-06 21:09 dompat-hardy.sys
501M -rw-r----- 1 root root 501M 2007-01-07 16:40 dompat.swap

Where the first size column is the effective space taken on disk while the second size column is the max space of that file. We see that dompat.data is sparse, since its max size is 24 GB while it takes only 12 GB on disk.

References

  • More information on command which can handle sparse files in

XRB’s Blog

Random thoughts of a Software Engineer

Patrick Jayet

Written by

Polyglot (FR, DE, EN, ES), polyglot programmer (Java, Groovy, Ruby, Swift, Objective-C, Scala, Python, O’Caml) polyglot methodologist (Scrum, Kanban, Lean).

XRB’s Blog

Random thoughts of a Software Engineer