Displaying allocated file size vs. file size reported by the file sytem
You can display the allocated file size, that is, how much disk space test.txt
takes up in the file system, with this command:
du -h test.txt
In contrast, the file size reported by the file system may or may not coincide with the allocated file size. To display it, use this command:
ls -l test.txt
(You can use ls -lh test.txt
instead if you prefer the file size to be more readable.)
Sparse files
How come test.txt
takes up only 4KB of disk space, but the file system reports that its size is 5GB?
This is a clever functionality that (most) file systems offer: instead of spending time and disk space to create a file that contains no data, it is more efficient to create an empty file, set its metadata to report 5BG of size, and only allocate disk space when actual data is added to the file.
And that's what the -n
option of mkfile
is all about:
If you use mkfile -n
, you will create a sparse file, which is somewhat similar to buying a train ticket without booking a seat. The train company won't reserve any seat for you (that is, no seat is allocated).
However, if you use mkfile
without the -n
option, for example:
mkfile 5g test.txt
the file will be created and 5GB will be allocated, in a way similar to buying a train ticket and booking a seat: the seat is reserved for you and nobody except you can use it (that is, the seat is allocated).
Summarizing, when creating a file, the disk space can be either:
- be allocated (that is, reserved in the file system so that no other file can use it) which, for example, may help prevent the file from becoming fragmented
- or, alternatively, you can request that the disk space not be allocated, which will create a sparse file. A sparse file is efficient, because it only occupies disk space when it contains data.
As pointed out by nohillside in their comment, you can "fill" the sparse file with data using dd
, for example:
dd if=/dev/zero of=test.txt bs=1024k count=5120
so that the allocated file size and the file size reported by the file system match.
For more information on sparse files, see: https://en.wikipedia.org/wiki/Sparse_file.
-n
says: but disk blocks aren't allocated until data is written to them.dd bs=1024k count=5120 if=/dev/zero of=myfile
.ls -l test.txt
:-rw------- 1 jaume staff 5368709120 Mar 5 11:55 test.txt
orls -lh test.txt
, if your prefer a more readable output:-rw------- 1 jaume staff 5.0G Mar 5 11:55 test.txt
.