How to create 1GB zeroed sparse file

A sparse file is a type of file that attempts to use file system space more efficiently when blocks allocated to the file are mostly empty. This is achieved by writing brief information (metadata) representing the empty blocks to disk instead of the actual empty space which makes up the block, using less disk space. The full block size is written to disk as the actual size only when the block contains real (non-empty) data.

When reading sparse files, the file system transparently converts metadata representing empty blocks into real blocks filled with zero bytes at runtime. The application is unaware of this conversion.

  • Solaris and Linux
    To create 1G sparse-file under Solaris or Linux we can use the dd command
    $ dd if=/dev/zero of=sparse-file bs=1k count=0 seek=1G
    $ du -b sparse-file
    549755813888    sparse-file
    $ du -B1 sparse-file
    0       sparse-file
  • Windows
    To create a 1G sparse file under Windows, we use the fsutil command:
    C:\> fsutil file createnew sparse-file 0x100000
    C:\> fsutil sparse setflag sparse-file
    C:\> fsutil sparse setrange sparse-file 0 0x100000
    NOTE: Whatever you write to the file (even ranges of zero) would be considered data to the operating system (and thus space allocated) and only FSCTL_SET_ZERO_DATA() would allow non-space occupying zeros to be written.
    fsutil sparse setflag [file]
    would allow a file to be set as sparse file.