sparse file

a sparse file is a type of computer 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 (i.e. sparse files contain blocks of zeros whose existance is recorded, but have no space allocated on disk).

When reading sparse files, the file system transparently converts metadata representing empty blocks into "real" blocks filled with zero bytes at runtime.


The advantage of sparse files is that storage is only allocated when actually needed: disk space is saved, and large files can be created even if there is insufficient free space on the file system.


Disadvantages are that sparse files may become fragmented; file system free space reports may be misleading

Copying with `cp'

Normally, `cp' is good at detecting whether a file is sparse, so it suffices to run:

cp file.img new_file.img


# du -h file.img
33M     file.img

# tar -cf file.tar file.img

# du -h file.tar
513M    file.tar

Luckily for you, though, tar has a `--sparse' (`-S') flag, that when used in conjunction with the `--create' (`-c') operation, tests all files for sparseness while archiving. If tar finds a file to be sparse, it uses a sparse representation of the file in the archive.

Creative Commons license icon Creative Commons license icon