Thomas' Tech Tips

Using sparse files in Linux

27 March 2022 - Thomas Damgaard

Sparse files is a file that takes up a certain amount of space on the file system while being wholly or partially empty.

In Linux, sparse files can be created using the fallocate utility.

The benefit is that the file is created instantly. No matter what size it has. No need to use dd to actually write X amount of bytes to the files. Instead, fallocate just tells the filesystem to create a file from here, to here.

Example:

fallocate -l 1000M sparse.dat

This will create a file called sparse.dat with the size 1000M. The file itself will not actually have 1000M bytes written to it.

If you read the file, it will contain zeros. Example:

$ fallocate -l 1000M sparse.dat
$ hd sparse.dat
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
*
3e800000

If fallocate is not available, you can also create sparsefiles using dd by running:

dd if=/dev/zero of=sparse_file bs=1 count=0 seek=1000M

Copying sparse files

One thing to be aware of is that if you read a sparse file with a program that is not aware that it is a sparse file, it will read the entire file including the sparse sections which will be read as zeros.

So when you have to copy a sparse file, you need to use a program that supports sparse files. Luckily most standard tools do.

Examples:

$ cp --sparse=always sparse.dat copy.dat
$ rsync --sparse sparse.dat copy.dat
$ tar --sparse -c -f sparse.tar sparse.dat
Filed under: linux, tips

Back to article list