World Library  
Flag as Inappropriate
Email this Article

Sparse file

Article Id: WHEBN0002631038
Reproduction Date:

Title: Sparse file  
Author: World Heritage Encyclopedia
Language: English
Subject: NTFS, Linear Tape File System, Apache HBase, SpartaDOS X, Lightning Memory-Mapped Database
Collection: Computer Files
Publisher: World Heritage Encyclopedia

Sparse file

A sparse file: The empty bytes don't need to be saved, thus they can be represented by metadata.

In computer science, a sparse file is a type of computer file that attempts to use file system space more efficiently when the file itself is 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.

Most modern file systems support sparse files, including most Unix variants and NTFS.[1] Apple's HFS+ does not provide for sparse files, but in OS X, the virtual file system layer supports storing them in any supported file system, including HFS+. Sparse files are commonly used for disk images, database snapshots, log files and in scientific applications.


  • Advantages 1
  • Disadvantages 2
  • Sparse files in Unix 3
    • Creation 3.1
    • Detection 3.2
    • Copying 3.3
    • Via Standard Input 3.4
  • See also 4
  • References 5
  • External links 6


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. This also reduces the time of the first write as the system doesn't have to allocate blocks for the "skipped" space. Since initial allocation usually requires the system to write all zeros to the space, it also keeps the system from having to write over the "skipped" space twice.


Disadvantages are that sparse files may become fragmented; file system free space reports may be misleading; filling up file systems containing sparse files can have unexpected effects (such as disk-full or quota-exceeded errors when merely overwriting an existing portion of a file that happened to have been sparse); and copying a sparse file with a program that does not explicitly support them may copy the entire, uncompressed size of the file, including the zero sections which are not allocated on disk—losing the benefits of the sparse property in the file. Sparse files are also not fully supported by all backup software or applications. However, the VFS implementation sidesteps the prior two disadvantages. Loading executables on Windows (exe or dll) which are sparse takes a much longer time, since the file cannot be memory mapped and not cached.

Sparse files in Unix

Sparse files are typically handled transparently to the user. But the differences between a normal file and sparse file become apparent in some situations.


The Unix command

 dd of=sparse-file bs=1k seek=5120 count=0

will create a file of five mebibytes in size, but with no data stored on disk (only metadata). (GNU dd has this behaviour because it calls ftruncate to set the file size; other implementations may merely create an empty file.)

Similarly the truncate command may be used, if available:

 truncate -s 5M 


The -s option of the ls command shows the occupied space in blocks.

 ls -ls sparse-file

Alternatively, the du command prints the occupied space, while ls prints the apparent size. In some non-standard versions of du, the option --block-size=1 prints the occupied space in bytes instead of blocks, so that it can be compared to the ls output:

 du --block-size=1 sparse-file
 ls -l sparse-file


Normally, the GNU version of cp is good at detecting whether a file is sparse, so

cp sparse-file new-file

creates new-file, which will be sparse. However, GNU cp does have a --sparse=WHEN option.[2] This is especially useful if a file containing long zero blocks is saved in a non-sparse way (i.e. the zero blocks have been written out to disk in full). Disk space can be saved by doing:

cp --sparse=always file1 file1_sparsed

Some cp implementations, like FreeBSD's cp, do not support the --sparse option and will always expand sparse files. A partially viable alternative on those systems is to use rsync with its own --sparse option[3] instead of cp. Unfortunately --sparse cannot be combined with --inplace, so rsyncing huge files across the network will always be wasteful of either network bandwidth or disk bandwidth.

Via Standard Input

 cp --sparse=always /proc/self/fd/0 new-sparse-file < somefile

See also


  1. ^ Giampaolo, Dominic (1999). Practical File System Design with the Be File System. Morgan Kaufmann. ISBN 1-55860-497-9
  2. ^ GNU cp: accept new option to control creation of sparse files
  3. ^ rsync: handle sparse files efficiently

External links

  • NTFS Sparse Files For Programmers
  • Creating sparse files in Windows XP Professional using fsutil
  • Creating sparse files in Solaris using mkfile
  • Understanding Sparse File Sizes in Database Snapshots
  • Detecting holes in sparse files
  • issue.--inplace and --sparsevirtsync is a commercial solution to rsync's
  • SparseChecker - Utility that allows to manage the sparse files on NTFS file system
  • Phantom - a program to convert files to sparse files to reduce disk space
This article was sourced from Creative Commons Attribution-ShareAlike License; additional terms may apply. World Heritage Encyclopedia content is assembled from numerous content providers, Open Access Publishing, and in compliance with The Fair Access to Science and Technology Research Act (FASTR), Wikimedia Foundation, Inc., Public Library of Science, The Encyclopedia of Life, Open Book Publishers (OBP), PubMed, U.S. National Library of Medicine, National Center for Biotechnology Information, U.S. National Library of Medicine, National Institutes of Health (NIH), U.S. Department of Health & Human Services, and, which sources content from all federal, state, local, tribal, and territorial government publication portals (.gov, .mil, .edu). Funding for and content contributors is made possible from the U.S. Congress, E-Government Act of 2002.
Crowd sourced content that is contributed to World Heritage Encyclopedia is peer reviewed and edited by our editorial staff to ensure quality scholarly research articles.
By using this site, you agree to the Terms of Use and Privacy Policy. World Heritage Encyclopedia™ is a registered trademark of the World Public Library Association, a non-profit organization.

Copyright © World Library Foundation. All rights reserved. eBooks from Project Gutenberg are sponsored by the World Library Foundation,
a 501c(4) Member's Support Non-Profit Organization, and is NOT affiliated with any governmental agency or department.