2

I'm on a Macbook Pro running macOS 12.2.1 (so using the APFS filesystem.)

I noticed some strange behavior when creating a large "dummy file" using the mkfile utility. (/usr/sbin/mkfile)

I created a 5 GB file with the command:

mkfile -n 5g test.txt

Then, to confirm it worked, I ran the du command. (/usr/bin/du).

du -hs ./test.txt

But I got an unexpected outcome:

4.0K    ./test.txt

I went to the file in Finder and looked at the file info, and saw there were two different sizes. Top line highlighted displays the size as 5 Gigabytes. However, further down it displays the text: "4 KB on disk"

After some further digging, I found this info on the mkfile man page:

OPTIONS
     -n     Create an empty filename.  The size is noted, but disk blocks aren't
            allocated until data is written to them.

So I wanted to know, is there a command that will let me see the allocated file size?

5
  • 3
    4k is the allocated file size. As the description for -n says: but disk blocks aren't allocated until data is written to them.
    – nohillside
    Commented Mar 4, 2022 at 20:40
  • 2
    Probably unrelated, but I would most likely use something like dd bs=1024k count=5120 if=/dev/zero of=myfile.
    – nohillside
    Commented Mar 4, 2022 at 20:43
  • 1
    Welcome to Ask Different. You can see the requested, not yet allocated size with ls -l test.txt: -rw------- 1 jaume staff 5368709120 Mar 5 11:55 test.txt or ls -lh test.txt, if your prefer a more readable output: -rw------- 1 jaume staff 5.0G Mar 5 11:55 test.txt.
    – jaume
    Commented Mar 5, 2022 at 10:56
  • 1
    @jaume That worked perfectly. The other solution also worked, so if either of you guys want to add that as an answer, I'd be happy to accept! Commented Mar 5, 2022 at 17:42
  • 1
    @EliRichardson Thank you for your feedback! I took up the gauntlet and added an answer :-) (including some background information about what's happening behind the scenes...)
    – jaume
    Commented Mar 6, 2022 at 13:37

1 Answer 1

1

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.

1
  • 1
    Thanks so much! Exactly what I was looking for! Commented Mar 6, 2022 at 21:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .