Sparse Files in Linux
Sparse files in Linux provide the ability to avoid allocating disk space for the null bytes within a file. A common example of sparse files is the /var/log/lastlog file, which keeps track of the last login for a user, while keeping the file organized by UIDs.
To create a spare file of our own, let’s first create a file with zeroes:
dd bs=512 count=20 if=/dev/zero of=/tmp/myfile1.dat
ls -lh /tmp/myfile1.dat
ls -s /tmp/myfile1.dat # The -s shows the actual size
After creating a file with only zeroes, we see that the file occupies space on the disk. Let’s now dig holes in the file with fallocate -d <filename>
.
fallocate -d /tmp/myfile1.dat
ls -s /tmp/myfile1.dat
cp --sparse=auto /tmp/myfile1.dat /tmp/myfile2.dat
# cp with --sparse=always does the equivalent of fallocate -d
ls -s /tmp/myfile2.dat # myfile2 is also created as a sparse file
Some file systems, such as vFAT are not supported. xfs and ext4 are among the popular file systems that are supported.