稀疏文件
在linux下,你可以创建所谓的sparse files。它们是完整的 NULL 块并不真正存在的文件!
试试这个:
$ dd if=/dev/zero count=2048 of=normalfile
2048+0 records in
2048+0 records out
1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.0103269 s, 102 MB/s
和
$ dd if=/dev/zero count=0 seek=2048 of=sparsefile
0+0 records in
0+0 records out
0 bytes copied, 0.000182708 s, 0.0 kB/s
然后
$ ls -l sparsefile normalfile
-rw-r--r-- 1 user user 1048576 Feb 3 17:53 normalfile
-rw-r--r-- 1 user user 1048576 Feb 3 17:53 sparsefile
$ du -b sparsefile normalfile
1048576 sparsefile
1048576 normalfile
但是
$ du -k sparsefile normalfile
0 sparsefile
1024 normalfile
$ du -h sparsefile normalfile
0 sparsefile
1.0M normalfile
sparsefile 中的这么长块没有被使用,它们不会被分配!
$ du -k --apparent-size sparsefile normalfile
1024 sparsefile
1024 normalfile
然后
$ diff sparsefile normalfile
echo $?
0
这两个文件几乎没有区别!
进一步
$ /sbin/mkfs.ext4 sparsefile
mke2fs 1.44.5 (15-Dec-2018)
Filesystem too small for a journal
...
Writing superblocks and filesystem accounting information: done
$ ls -l sparsefile normalfile
-rw-r--r-- 1 user user 1048576 Feb 3 17:53 normalfile
-rw-r--r-- 1 user user 1048576 Feb 3 17:59 sparsefile
$ du -k sparsefile
32 sparsefile
$ diff sparsefile normalfile
Binary files sparsefile and normalfile differ