Creating a sparse file on an APFS (Apple File System) volume from the command line in macOS can be done using the mkfile or dd command. However, APFS supports sparse files, but not all tools behave the same way on APFS as they do on other filesystems like HFS+.
Here’s how you can create a sparse file on APFS:
✅ Method 1: Using mkfile (Recommended for Sparse Files)
The mkfile command is a macOS-specific utility that creates files of a specified size, and it creates sparse files by default when supported by the filesystem.
Syntax:
mkfile [-nv] size[file_unit] filename
Example:
To create a 1GB sparse file named sparsefile.dmg:
mkfile 1g sparsefile.dmg
1g = 1 gigabyte
- Other units:
b (bytes), k (kilobytes), m (megabytes), g (gigabytes)
Check if it's sparse:
Use ls -l and du to verify it's sparse:
ls -l sparsefile.dmg
du -h sparsefile.dmg
If it's sparse, du will show a much smaller size than the actual file size reported by ls.
✅ Method 2: Using dd with Sparse Option
You can also use dd, but you need to write only blocks that are non-zero. This doesn't guarantee a sparse file unless you're careful.
Example:
dd if=/dev/zero of=sparsefile.dmg bs=1m count=0 seek=1024
bs=1m = block size of 1MB
count=0 = don’t copy any blocks
seek=1024 = skip ahead 1024 blocks (i.e., 1 GB)
- This creates a 1GB file that is sparse, because no data was actually written.
Again, check with:
ls -l sparsefile.dmg
du -h sparsefile.dmg
⚠️ Note: dd does not explicitly support creating sparse files like mkfile does; however, this method works on APFS because the filesystem detects and optimizes empty space.
❌ Avoid: /dev/urandom or full writes with dd
Avoid doing something like:
dd if=/dev/urandom of=sparsefile.dmg bs=1m count=1024
This writes real data to every block and will not result in a sparse file — it will consume full disk space.
Summary
| Method | Creates Sparse File on APFS? | Notes |
|---|
mkfile | ✅ Yes | Recommended, simple, macOS-native |
dd with count=0 seek=N | ✅ Yes | Works, but less intuitive |
dd writing /dev/zero over full size | ❌ No | Fills file fully, no sparseness |
Let me know if you want to create a sparse bundle disk image or mount it as a virtual disk too!