brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: mkfile makes a test file of an exact size on a Mac

Terminal
~/tips/tip-macos-mkfile-n-test-file

mkfile -v 100m /tmp/test-100m.bin

Creates a file of an exact size, optionally without allocating the disk space.

For testing a share, a backup target or an upload limit I want a file of a known size, without hunting for something big to copy.

macOS ships mkfile for that:

mkfile -v 100m /tmp/test-100m.bin

The size takes a suffix: b for 512 bytes, k, m or g, and plain bytes if you leave it out. -v reports the name and size of each file it creates. By default the file is padded with zeroes, so it really occupies the space.

With -n the size is noted, but the man page says disk blocks are not allocated until data is written to them:

mkfile -n 10g /tmp/sparse-10g.bin

Compare the two views of that file with ls -lh /tmp/sparse-10g.bin and du -h /tmp/sparse-10g.bin. The first shows the size you asked for, the second what the disk actually holds. It is a quick way to check how a tool handles a very large file without using the space.

You can list several files in one command, each with the same size, and clean up afterwards with rm /tmp/test-100m.bin /tmp/sparse-10g.bin. Check the names before you press Return, because rm does not ask.

The caveat is that these files are zeros. A transfer that compresses on the way will look much faster than it would with real data, and a sparse file with -n has no data behind it at all, so it says little about disk speed. The man page itself describes mkfile as a tool for files used as NFS-mounted swap areas, so using it for test files is a side use.

Further reading: mkfile man page

More Terminal tips ioreg -rn AppleSmartBattery | grep CycleCount Terminal can save your windows and tabs as a window group Export a Terminal profile to a .terminal file and import it on another Mac mc hidutil property --set '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}' All Terminal tips (116) All topics

Link